MySQL常用SQL Statement Syntax

xiaoxiao2021-03-01  6

Data Definition Statements(数据定义语法)

1.CREATE TABLE Syntax

从存在的表里创建具有一样表结构的新表 语法:CREATE TABLE ... LIKE Syntax 实例: 创建一个表 CREATE TABLE `user` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名字', `age` tinyint(4) NOT NULL DEFAULT '0' COMMENT '年龄', PRIMARY KEY (`uid`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; 使用该命令 CREATE TABLE user_two LIKE user; 结果如下:

从存在的表里创建具有一样表结构的新表同时原来的表的数据也填充到新表 语法:CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl; 实例: INSERT INTO `user`(`name`, `age`) VALUES ('JACK',11),('ROSE',12) 执行该命令:CREATE TABLE user_three SELECT * FROM user; 结果如下:

也可以去原来表的部分字段填充 mysql> SELECT * FROM foo; +---+ | n | +---+ | 1 | +---+ mysql> CREATE TABLE bar (m INT) SELECT n FROM foo; Query OK, 1 row affected (0.02 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM bar; +------+---+ | m | n | +------+---+ | NULL | 1 | +------+---+ 1 row in set (0.00 sec)

2.RENAME TABLE Syntax

修改表的名称 RENAME TABLE old_table TO new_table; That statement is equivalent to the following ALTER TABLE statement等同: ALTER TABLE old_table RENAME new_table;

Data Manipulation Statements(数据操作语法)

1.INSERT Syntax

从现有表筛选数据插入到另一个表中 语法: INSERT [LOW_PRIORITY | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [PARTITION (partition_name [, partition_name] ...)] [(col_name [, col_name] ...)] SELECT ... [ON DUPLICATE KEY UPDATE assignment_list] value: {expr | DEFAULT} assignment: col_name = value assignment_list: assignment [, assignment] ... With INSERT ... SELECT, you can quickly insert many rows into a table from the result of a SELECT statement, which can select from one or many tables 实例: mysql> SELECT * FROM user_three; +-----+--------+-----+ | uid | name | age | +-----+--------+-----+ | 10 | JACKgg | 15 | | 11 | ROSEgg | 16 | +-----+--------+-----+ 2 rows in set (0.00 sec) mysql> SELECT * FROM user; +-----+------+-----+ | uid | name | age | +-----+------+-----+ | 10 | JACK | 11 | | 11 | ROSE | 12 | +-----+------+-----+ 2 rows in set (0.00 sec) mysql> INSERT INTO `user`(`name`,`age`) SELECT `name`,`age` FROM user_three; Query OK, 2 rows affected (0.03 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM user; +-----+--------+-----+ | uid | name | age | +-----+--------+-----+ | 10 | JACK | 11 | | 11 | ROSE | 12 | | 12 | JACKgg | 15 | | 13 | ROSEgg | 16 | +-----+--------+-----+ 4 rows in set (0.00 sec) 注意插入的字段顺序一定要和后面查询出来的字段顺序一致 语法:INSERT ... ON DUPLICATE KEY UPDATE Syntax If you specify an ON DUPLICATE KEY UPDATE clause and a row to be inserted would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row occurs.(如果在唯一或者主键字段存在该值则为更新,否则为插入) 实例: mysql> INSERT INTO `user`(`uid`,`name`,`age`) VALUES ('9','TOM','22') ON DUPLICA TE KEY UPDATE name='TOM',age=22; Query OK, 1 row affected (0.08 sec) mysql> SELECT * FROM user; +-----+--------+-----+ | uid | name | age | +-----+--------+-----+ | 9 | TOM | 22 | | 10 | JACK | 11 | | 11 | ROSE | 12 | | 12 | JACKgg | 15 | | 13 | ROSEgg | 16 | +-----+--------+-----+ 5 rows in set (0.00 sec) mysql> INSERT INTO `user`(`uid`,`name`,`age`) VALUES ('19','JOHN','20') ON DUPLI CATE KEY UPDATE name='JOHN',age=20; Query OK, 1 row affected (0.03 sec) mysql> SELECT * FROM user; +-----+--------+-----+ | uid | name | age | +-----+--------+-----+ | 9 | TOM | 22 | | 10 | JACK | 11 | | 11 | ROSE | 12 | | 12 | JACKgg | 15 | | 13 | ROSEgg | 16 | | 19 | JOHN | 20 | +-----+--------+-----+ 6 rows in set (0.00 sec)
转载请注明原文地址: https://www.6miu.com/read-3850006.html

最新回复(0)