MySQL 操作语句

解释:|:或;{}:必选;[]:可选

 

创建数据库并指定字符编码:

CREATE {DATABASE|SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name

 

创建表并指定储存引擎和字符编码:

CREATE TABLE [IF NOT EXISTS] tbl_name(column_name,......)ENGINE=engine_name [DEFAULT] CHARACTER SET=character_name

 

给表添加注释:

ALTER TABLE tbl_name COMMENT 'comment_name'

 

 

查看数据库引擎

SHOW ENGINES

 

查看当前服务器下的数据表列表

SHOW {DATABASES|SCHEMAS} [LIKE 'pattern'|WHERE expr]

 

查看警告信息

SHOW WARNINGS

 

修改数据库

ALTER {DATABASE|SCHEMA} [db_name] [DEFAULT] CHARACTER SET [=] chanset_name

 

删除数据库

DROP {DATABASE|SCHEMA} [IF EXISTS] db_name

 

使用数据库

USE 数据库名称

 

当前数据库

SELECT DATABASE()

  

创建数据表

CREATE TABLE [IF NOT EXISTS] table_name(column_name data_type,......)

 

查看数据表列表

SHOW TABLES [FROM db_name] [LIKE 'pattern'|WHERE expr]

 

查看数据表结构

SHOW COLUMNS FROM tab_name

 

插入记录

INSERT [INTO] tbl_name [(col_name,.......)] VALUES(val,...)[,(val,...),...]

 

记录查找

SELECT col_name,... FROM tbl_name

 

添加单列

ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST|AFTER col_name]

 

添加多列

ALTER TABLE tbl_name ADD [COLUMN] (col_name column_definition,......)

 

删除列

ALTER TABLE tbl_name DROP [COLUMN] col_name

 

添加主键约束

ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,......)

 

添加唯一约束

ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_name,......)

 

添加外键约束

ALTER TABLE tb_name ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,......) reference_definition

 

添加/删除默认约束

ALTER TABLE tbl_name ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}

 

删除主键约束

ALTER TABLE tbl_name DROP PRIMARY KEY

 

删除唯一约束

ALTER TABLE tbl_name DROP {INDEX|KEY} index_name

 

删除外键约束

ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol

 

修改列定义

ALTER TABLE tbl_name MODIFY [COLUMN] col_name column_definition [FIRST|AFTER col_name]

 

修改列名称

ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]

 

数据表更名

1)ALTER TABLE tbl_name RENAME [TOIAS] new_tbl_name
2)RENAME TABLE tbl_name TO new_tbl_name [,tbl_name2 TO new_tbl_name]........

 

插入记录

1)INSERT [INTO] tbl_name[(col_name,......)] {VALUES|VALUE} ({expr|DEFAULT},......),(...)...
2)INSERT [INTO] tbl_name SET col_name={expr|DEFAULT},.....
3)INSERT [INTO] tbl_name[(col_name,......)] SELECT .......

 

UPDATE更新表

UPDATE [LOW_PRIORITY] [IGNORE] tbl_reference SET col_name1={expr|DEFAULT} [,col_name2={expr|DEFAULT}]...[WHERE where_condition]

 

DELETE删除表

DELETE FROM tbl_name [WHERE where_condition]

 

SELECT查询表

SELECT select_expr[,select_expr....]
[FROM tbl_references 
[WHERE ehere_condition]
[GROUP BY {col_name|position} [ASC|DESC],...]
[HAVING where_condition]
[ORDER BY {col_name|expr|position} [ASC|DESC],...]
[LIMIT {[offset,] row_count |row_count OFFSET offset}]]

1)查询表达式:每一个表达式表示想要的一列,必须有至少一个,多个列之间以英文逗号分隔,星号(*)表示所以列,tbl_name.*可以表示命名表的所有列,查询表达式可以使用[AS] ALIAS_NAME为其赋予别名,别名可用于GROUP BY,ORDER BY或HAVING子句

 

分页公式: (当前页码-1)*每页显示的记录数

 

多表更新

UPDATE tbl_name SET col_name={expr|DEFAULT}[,col_name2={expr|DEFAULT}]... [WHERE where_condition]

 

创建表同时写入数据(CREATE....SELECT)

CREATE TABLE [IF NOT TXISTS] tbl_name [(create_definition,....)] select_statement

 

多表删除

DELETE FROM tbl_name[.*] [,tbl.name[.*]].... FROM tbl_references [WHERE where_condition]

 

posted @ 2017-05-16 20:54  星小梦  阅读(207)  评论(0编辑  收藏  举报