mysql数据库基本查询语句汇总
设置数据库编码:
SET NAMES 字符集;
创建数据库:
CREATE DATABASE db_name;
CREATE DATABASE db_name CHARACTER SET 字符集;
使用数据库:
USE db_name;
删除数据库:
DROP db_name;
创建数据表:
CREATE TABLE tb_name(
col-name1,
col_name2,
……
);
==注:创建数据表的时候,同时插入另外一张表的数据;
CREATE TABLE tb_name(columns_define)select_statement
删除数据表:
DELETE FROM tb_name;
DELETE FROM tb_name [WHERE condition];
可以在不删除表的情况下删除所有的行。这意味着表的结构、属性和索引都是完整的:
显示数据库、表:
SHOW DATABASES;
SHOW CREATE DATABASE db_name ;
SHOW TABLE STATUS FROM db_name;
SHOW TABLES;
SHOW CREATE TABLE tb_name ;
SHOW TABLES FROM db_name;
SHOW COLUMNS FROM tb_name(DECRIBE tb_name,EXPLAIN tb_name);
SHOW INDEX FROM tb_name;
SHOW TABLE STATUS;
显示错误(警告)信息:
SHOW WARNINGS;
修改数据库、表:
ALTER DATABASE db_name CHARACTER SET 字符集;
==修改列定义==:ALTER TABLE tb_name ADD col_name ==col_define(列定义)== [FIRST | AFTER col_name];
ALTER TABLE tb_name DROP col_name;
ALTER TABLE tb_name DROP col_name1,DROP col_name2,DROP col_name3,……;
ALTER TABLE tb_name MODIFY col_name type;
ALTER TABLE tb_name CHANGE column col_name new_col_name;
ALTER TABLE tb_name RENAME new_tb_name(==或者RANAME TABLE tb_name1 TO tb_name2==);
DROP TABLE tb_name,……;
DROP TABLE IF EXISTS tb_name(首选方式);
DROP TABLE db_name.tb_name;
ALTER TABLE tb_name ADD [CONSTRAINT ==con_name(约束名)==] PPIMARY KEY (col_name);
ALTER TABLE tb_name ADD [CONSTRAINT ==con_name(约束名)==] UNIQUE(col_name);
ALTER TABLE tb_name ADD [CONSTRAINT ==con_name(约束名)==] FOREIGN KEY (col_name) REFERRNCES ==col_define(列定义)==;
==ALTER== TABLE tb_name ==ALTER== [COLUMN] col_name {SET DEFAULT liter | DROP DERAULT}
ALTER TABLE tb_name DROP PRIMARY KEY;
ALTER TABLE tb_name DROP { INDEX | KEY } index_name;
ALTER TABLE tb_name DROP FOREIGN KEY ==约束名==;
插入数据:
INSERT [INTO] tb_name VALUES(==DEFAULT==,"",""……);
INSERT [INTO] tb_name VALUES(==NULL==,"",""……);
INSERT [INTO] tb_name VALUES("","",""……),("","",""……)…… ; //一次性插入多条数据;
INSERT [INTO] tb_name VALUES("","",""……); //可以和DESCRIBE tb_name来联合查看列的次序,以便于向里面顺序插入数据;
INSERT [INTO] tb_nmae(COLUMNS,……) VALUES("","",""……);
INSERT [INTO] tb_name(COLUMN) VALUES(""); //向单列插入一个数据;
INSERT [INTO] tb_name(COLUMN) VALUES(""),(""); //向单列插入多行数据;
INSERT [INTO] tb_name SET col_name = "",col_name = "",……; //向指定的列中插入单条记录(==不推荐使用,但是可以配合子查询来使用==);
INSERT [INTO] tb_name(col_name,……) SELECT col_name FROM tb_name [WHERE condintion],[ORDER BY condition],……;
查询数据:
SELECT TOP number(*,col_name(col_names)) FROM tb_name
WHERE primary_constraint
GROUP BY grouping_columns //对结果进行分组;
GROUP BY{ col_name | position } [ASC | DESC,……] ;
ORDER BY soeting_columns //对结果进行排序;
HAVING secondary_constraint
LIMIT position;
LIMIT position1,position2;
更新数据:
UPDATE tb_name SET col_name = {value | expression},col_name = {value | expression},…… [WHRER condition];==(单表更新)==
-
结束语…………………………福利时间到………………………………
大家同为程序员,在这里给大家真诚的送上福利。