1、insert into插入语句
INSERT INTO 语句
INSERT INTO 语句用于向表格中插入新的行。
语法
INSERT INTO 表名称 VALUES (值1, 值2,…)
不指定列名需要在values中指定所有列对应的值,顺序不能错
我们也可以指定所要插入数据的列:
INSERT INTO table_name (列1, 列2,…) VALUES (值1, 值2,…)
一次插入多行,每行用逗号隔开,写法仅限于MYSQL数据库
Insert into 表名 values(列1对应值,列2对应值,…,列N对应值),(列1对应值,列2对应值,…,列N对应值),(列1对应值,列2对应值,…,列N对应值)…;
插入新的行
“Persons” 表:
| LastName | FirstName | Address | City |
| — | — | — | — |
| Carter | Thomas | Changan Street | Beijing |
SQL 语句:
INSERT INTO Persons VALUES (‘Gates’, ‘Bill’, ‘Xuanwumen 10’, ‘Beijing’)
结果:
| LastName | FirstName | 6 | City |
| — | — | — | — |
| Carter | Thomas | | Beijing |
| Gates | Bill | | Beijing |
在指定的列中插入数据
“Persons” 表:
| LastName | FirstName | Address | City |
| — | — | — | — |
| Carter | Thomas | Changan Street | Beijing |
| Gates | Bill | Xuanwumen 10 | Beijing |
SQL 语句:
INSERT INTO Persons (LastName, Address) VALUES (‘Wilson’, ‘Champs-Elysees’)
结果:
| LastName | FirstName | Address | City |
| — | — | — | — |
| Carter | Thomas | Changan Street | Beijing |
| Gates | Bill | Xuanwumen 10 | Beijing |
| Wilson | | Champs-Elysees | |
2、update修改语句
Update 语句
Update 语句用于修改表中的数据。
语法:
UPDATE 表名称 SET 列1名称 = 列1新值, 列2名称 = 列2新值 WHERE 列名称 = 某值
Person:
| LastName | FirstName | Address | City |
| — | — | — | — |
| Gates | Bill | Xuanwumen 10 | Beijing |
| Wilson | | Champs-Elysees | |
更新某一行中的一个列
我们为 lastname 是 “Wilson” 的人添加 firstname:
UPDATE Person SET FirstName = ‘Fred’ WHERE LastName = ‘Wilson’
结果:
| LastName | FirstName | Address | City |
| — | — | — | — |
| Gates | Bill | Xuanwumen 10 | Beijing |
| Wilson | Fred | Champs-Elysees | |
更新某一行中的若干列
我们会修改地址(address),并添加城市名称(city):
UPDATE Person SET Address = ‘Zhongshan 23’, City = ‘Nanjing’
WHERE LastName = ‘Wilson’
结果:
| LastName | FirstName | Address | City |
| — | — | — | — |
| Gates | Bill | Xuanwumen 10 | Beijing |
| Wilson | Fred | Zhongshan 23 | Nanjing |
3、delete删除语句
DELETE 语句
DELETE 语句用于删除表中的行。
语法
DELETE FROM 表名称 WHERE 列名称 = 值
Person:
| LastName | FirstName | Address | City |
| — | — | — | — |
| Gates | Bill | Xuanwumen 10 | Beijing |
| Wilson | Fred | Zhongshan 23 | Nanjing |
删除某行
“Fred Wilson” 会被删除:
DELETE FROM Person WHERE LastName = ‘Wilson’
结果:
| LastName | FirstName | Address | City |
| — | — | — | — |
| Gates | Bill | Xuanwumen 10 | Beijing |
删除所有行
可以在不删除表的情况下删除所有的行。这意味着表的结构、属性和索引都是完整的:
DELETE FROM table_name
或者:
DELETE * FROM table_name