SQL基础三
一、SQL ORDER BY 子句
ORDER BY 语句用于对结果集进行排序,默认按照升序对记录进行排序,如果需要按照降序进行排序,需要在后面追加关键字DESC。应用如下:
原始的表:Orders表
Company | OrderNumber |
---|---|
IBM | 3532 |
W3School | 2356 |
Apple | 4698 |
W3School | 6953 |
实例1:
以字母顺序显示公司名称:
SELECT Company, OrderNumber FROM Orders ORDER BY Company
Company | OrderNumber |
---|---|
Apple | 4698 |
IBM | 3532 |
W3School | 6953 |
W3School | 2356 |
实例2:
已字母顺序显示公司名称,并以数字顺序显示顺序号:
SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber
Company | OrderNumber |
---|---|
Apple | 4698 |
IBM | 3532 |
W3School | 2356 |
W3School | 6953 |
实例3:
以逆字母顺序显示公司名称:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC
Company | OrderNumber |
---|---|
W3School | 6953 |
W3School | 2356 |
IBM | 3532 |
Apple | 4698 |
实例4:
以逆字母顺序显示公司名称,并以数字顺序显示顺序号:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC
Company | OrderNumber |
---|---|
W3School | 2356 |
W3School | 6953 |
IBM | 3532 |
Apple | 4698 |
二、SQL INSERT INTO 语句
INSERT INTO语句用于向表格中插入新的行。
语法:
INSERT INTO 表名称 VALUES (值1, 值2,....)
也可以指定索要插入数据的列
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
应用实例:
插入新的行:
“Persons”表:
LastName | FirstName | Address | City |
---|---|---|---|
Carter | Thomas | Changan Street | Beijing |
SQL语句为:
INSERT INTO Persons VALUES('Gates', 'Bill', 'Xuanwumen 10', 'Beijing')
结果:
LastName | FirstName | Address | City |
---|---|---|---|
Carter | Thomas | Changan Street | Beijing |
Gates | Bill | Xuanwumen 10 | 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 |
三、SQL UPDATE 语句
Update语句用于修改表中的数据。
语法:
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
应用实例:
“Persons”表
LastName | FirstName | Address | City |
---|---|---|---|
Gates | Bill | Xuanwumen 10 | Beijing |
Wilson | Champs-Elysees |
更新某一行中的一列:
为 lastname 是 "Wilson" 的人添加 firstname:
UPDATE Persons 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 |
三、SQL DELETE 语句
DELETE语句用于删除表中的行。
语法:
DELETE FROM 表名称 WHERE 列名称 = 值
应用实例:
“Persons”表
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