1、创建表
2、 create table Person
3、 (
4、 id INT,
5、 LastName varchar(255),
6、 FirstName varchar(255),
7、 Address varchar(255),
8、 City varchar(255));
2、插入
insert into Person values(1,'Adams','John','Oxford Street','London');
insert into Person values(2,'Bush','George','Fifth Avenue','New York');
insert into Person (id,lastname,firstname,address,city) values(3,'Carter','Thomas','Changan Street','Beijing');
3、查询
select Lastname from person;
select Lastname,FirstName from person;
select * from person;
4、DISTINCT
create table Orders
(
Company varchar(255),
OrderNumber varchar(255)
);
insert into orders values('IBM','3532');
insert into orders values('W3School','2356');
insert into orders values('Apple','4698');
insert into orders values('W3School','6953');
select distinct company from orders;
5、WHERE
select * from person where city='Beijing';
6、AND OR
select * from person where firstname='Thomas' and lastname='Carter';
Select * from person where firstname='Thomas' or lastname='Carter';
select * from person where (firstname='Thomas' or Firstname='William') and lastname='Carter';
7、ORDER BY
select company ,orderNumber from orders order by company;
select company ,orderNumber from orders order by company,orderNumber;
select company ,orderNumber from Orders order by Company DESC;
select company ,orderNumber from orders order by Company DESC, OrderNumber ASC;
8、UPDATE
10、 insert into person (LastName, Address) VALUES ('Wilson', 'Champs-Elysees');
11、 update person set firstname='Fred' Where lastname='Wilson';
12、 update person set address='Zhongshan 23',City ='Nanjing' where lastname='Wilson';
9、DELETE
delete from 表名称 where 列名称=值;
# 在不删除表的情况下删除所有的行
delete from 表名称;
delete * from 表名称;
10、TOP 并非所有的数据库都支持TOP
select top 2 * from person;
select top 50 percent * from Person;
11、LIKE
SELECT * from person where city like 'N%'; --选取居住在以"N"开始的城市里的人
SELECT * from person where city like '%g'; --选举居住在以"g"结尾的城市里的人
SELECT * from person where city like '%Lon%';-- 选取居住在包含“Lon”的城市里的人
SELECT * from person where city not like '%Lon%';--选取居住在不包含“Lon”的城市里的人
SQL通配符如下
通配符 | 描述 |
% | 替代一个或多个字符 |
_ | 仅替代一个字符 |
[charlist] | 字符列表中的任何单一字符 |
[^charlist] [!charlist] | 不在字符列中的任何单一字符(并不是所有的数据库都支持) |
12、IN操作符
SELECT * FROM person where LastName in ('Adams','Carter');
13、BETWEEN
SELECT * FROM person where LastName BETWEEN 'Adams' AND 'Carter';--名字介于“Adams”(包括)和“Carter”(不包括)之间的人
SELECT * FROM person where LastName NOT BETWEEN 'Adams' AND 'Carter';
14、alias(别名)
SELECT po.OrderID,p.LastName,p.FirstName From Person AS p,Product_Orders AS po WHERE p.LastName='Adams' AND p.FirstName='John';
SELECT LastName AS Family, FirstName AS Name ROM Persons
15、JOIN
select person.lastname ,person.firstname,orders.orderNo from person ,orders where person.id =orders.id_p;
select person.lastname ,person.firstname,orders.orderNo from person inner join orders on person.id =orders.id_p order by person.lastname;
--JOIN(INNER JOIN):如果表中有至少一个匹配,则返回行
--LEFT JOIN:返回左表全部行和右表满足ON条件的行
--RIGHT JOIN:返回右表全部行和左表满足ON条件的行
--FULL JOIN:返回左右表全部的行
16、UNION
UNION操作符用于合并两个或多个SELECT语句的结果集, SELECT语句必须拥有相同数量的列,列也必须拥有相似的数据类型,并且顺序必须相同。UNION只会显示不同的值,如果允许重复的值,要使用UNION ALL。
select * from Employees_china union select * from employees_usa;
17、SELECT into
从一个表中选取数据,然后把数据插入另一个表中
SELECT lastname,firstname into persons_backup from person where city='Beijing';
select * from persons_backup;
18、SQL约束
NOT NULL:非空
UNIQUE:每个表可以用多个UNIQUE约束,但是每个表只能有一个PRIMARY KEY约束
PRIMARY KEY:每个表都应该有一个主键,并且每个表只能有一个主键
FOREIGN KEY:
CHECK:用于限制列中值的范围
DEFAULT:向列中插入默认值,如果没有规定其他的值,那么会将默认值添加到所有的新记录
19、创建索引
create index personIndex on Person(LastName);
20、alter
alter table person add birthday date;
alter table person alter birthday year;
alter table person drop column birthday;
21、auto increment
会在新记录插入表中时生成一个唯一的数字,每种数据库的具体语法有所不同
22、VIEW
CREATE VIEW [Current product list] AS SELECT ProductID,ProductName FROM Products WHERE Discontinued=No;
23、AVG
select AVG(id_o) from orders;
select * from orders where id_o>(select AVG(id_o) from orders);--找到大于平均值的订单
24、COUNT
SELECT COUNT(column_name) FROM table_name;--COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入):
SELECT COUNT(*) FROM table_name;--COUNT(*) 函数返回表中的记录数:
SELECT COUNT(DISTINCT column_name) FROM table_name;--COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目
25、MAX
select max(id_o) from orders; --查找最大值
26、MIN
select min(id_o) from orders; --查找最小值
27、SUM
select sum(id_o) from orders; --计算和
28、GROUP BY
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer;--查找每个客户的总金额
29、HAVING
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000;--查找订单总额少于2000的客户
SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Bush' OR Customer='Adams' GROUP BY Customer HAVING SUM(OrderPrice)>1500;--查找客户“Bush”或“Adams”拥有超过1500的订单总金额