MySQL常用基本操作。工作用了一段时间MySQL,整理了一下常用的基本命令。
CREATE TABLE IF NOT EXISTS `test` (
`col_1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`col_2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`col_3` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`date` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
drop table if exists test;
-- 删除多个表
drop table test1, test2, test3;
alter table table_name_1 rename table_name_2;
alter table table_name change column column_name_1 column_name_2 varchar(255);
alter table table_name
add column column_name varchar(255),
add column column_name2 varchar(255);
---- 增加列到特定位置:如添加 column_new列 到 column_4 这一列后面
alter table table_name add column column_new varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL after column_4;
---- 注意,如果想添加到第一列的话,可以用:
alter table table_name add column column_new varchar(255) not null first;
alter table table_name drop column column_name;
INSERT INTO dbname.table_name(col_1, col_2, col_3, date)
VALUES('中国', '28177', '-2117', '20230817');
delete from table_name where date=20230731;
update table_name set col_2='12345' where col_1='中国' and date=20230731;
UPDATE test_1 t1, test_2 t2
SET t1.col_2 = t2.col_5,
t1.col_3 = t2.col_6
WHERE t1.date = t2.date
AND t1.col_1 = t2.col_1;
SELECT
`TABLE_NAME`, `UPDATE_TIME`
FROM
`information_schema`.`TABLES`
WHERE
`information_schema`.`TABLES`.`TABLE_SCHEMA` = 'database_name'
AND
`information_schema`.`TABLES`.`TABLE_NAME` = 'table_name';
select Host,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv from user;
select Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv from db;