SQL 提纲

1.创建数据库
create database test2;
2.删除数据库
drop database test2;

3.创建表
create table ceshi
(
ids int auto_increment primary key,
uid varchar(20),
name varchar(20),
class varchar(20),
foreign key (class) references class(code)
);
create table class
(
code varchar(20) primary key,
name varchar(20) not null
);

**自增长 auto_increment
**主键 primary key
**外键 foreign key (列名) references 主表名(列名)
**非空 not null

4.删除表
drop table ceshi;

注意:
1.类型包含长度的在类型后面加括号,括号里面写长度
2.上一列写完加逗号
3.最后一列不要写逗号
4.在每一条SQL语句写完之后要加分号
5.如果有外键关系,先创建主表

创建表:
create table class
(
code varchar(20) primary key,
name varchar(20)
);
create table student
(
code varchar(20) primary key,
name varchar(20),
sex bit,
age int,
class varchar(20),
foreign key (class) references class(code)
);
create table kecheng
(
code varchar(20) primary key,
name varchar(20)
);
create table teacher
(
code varchar(20) primary key,
name varchar(20)
);
create table chengji
(
ids int auto_increment primary key,
scode varchar(20),
kcode varchar(20),
degree float,
foreign key (scode) references student(code),
foreign key (kcode) references kecheng(code)
);
create table tkecheng
(
ids int auto_increment primary key,
tcode varchar(20),
kcode varchar(20),
foreign key (kcode) references kecheng(code),
foreign key (tcode) references teacher(code)
);

查询:

1.简单查询

select * from Info --查所有数据
select Code,Name from Info --查指定列的数据
select Code as '代号',Name as '姓名' from Info --给列指定别名

2.条件查询

select * from Info where Code='p001'
select * from Info where Sex='true' and Nation='n001' --多条件并的关系
select * from Info where Sex='true' or Nation='n001' --多条件或的关系

3.范围查询

select * from Car where Price>40 and Price<50
select * from Car where Price between 40 and 50

4.离散查询

select * from Car where Code in ('c001','c005','c010','c015')
select * from Car where Code not in ('c001','c005','c010','c015')

5.模糊查询

select * from Car where Name like '%宝马%' --查包含宝马的
select * from Car where Name like '宝马%' --查以宝马开头的
select * from Car where Name like '%宝马' --查以宝马结尾的
select * from Car where Name like '宝马' --查等于宝马的

select * from Car where Name like '__E%' --查第三个字符是E的

% 代表是任意多个字符

_ 代表是一个字符

6.排序查询

select * from Car order by Price asc --以价格升序排列
select * from Car order by Price desc --以价格降序排列
select * from Car order by Oil desc,Price asc --以两个字段排序,前面的是主条件后面的是次要条件

7.分页查询

select top 5 * from Car
select top 5 * from Car where Code not in (select top 5 Code from Car)

当前页:page = 2; 每页显示:row = 10;

select top row * from Car where Code not in (select top (page-1)*row Code from Car)

8.去重查询

select distinct Brand from Car

9.分组查询

select Brand from Car group by Brand having count(*)>2

10.聚合函数(统计查询)

select count(*) from Car --查询所有数据条数
select count(Code) from Car --查询所有数据条数

select sum(Price) from Car --求和
select avg(Price) from Car --求平均
select max(Price) from Car --求最大值
select min(Price) from Car --求最小值


高级查询

1.连接查询

select * from Info,Nation --形成笛卡尔积

select * from Info,Nation where Info.Nation = Nation.Code

select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where Info.Nation = Nation.Code

select * from Info join Nation on Info.Nation = Nation.Code --join on 的形式

2.联合查询

select Code,Name from Info
union
select Code,Name from Nation

3.子查询

一条SQL语句中包含两个查询,其中一个是父查询(外层查询),另一个是子查询(里层查询),子查询查询的结果作为父查询的条件。

--查询民族为汉族的所有人员信息
select * from Info where Nation = (select Code from Nation where Name = '汉族')


(1)无关子查询

子查询可以单独执行,子查询和父查询没有一定的关系

--查询系列是宝马5系的所有汽车信息
select * from Car where Brand =(select Brand_Code from Brand where Brand_Name = '宝马5系')


(2)相关子查询

--查找油耗低于该系列平均油耗的汽车

select * from Car where Oil<(该系列的平均油耗)
select avg(Oil) from Car where Brand = (该系列)


select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)

1.注释语法:--,#
2.后缀是.sql的文件是数据库查询文件
3.保存查询
4.在数据库里面 列有个名字叫字段 行有个名字叫记录

CRUD操作:
create 创建(添加)
read 读取
update 修改
delete 删除

1、添加数据
insert into Info values('p009','张三',1,'n001','2016-8-30 12:9:8') ;
给特定的列添加数据
insert into Info (code,name) values('p010','李四');
自增长列的处理
insert into family values('','p001','数据','T001','数据',1);

insert into 表名 values(值)

2、删除数据
删除所有数据
delete from family
删除特定的数据
delete from Info where code='p001'

delete from 表名 where 条件

3、修改数据
修改所有数据
update Info set name='徐业鹏'
修改特定数据
update Info set name='吕永乐' where code='p002'
修改多列
update Info set name='吕永乐',sex=1 where code='p003'

update 表名 set 要修改的内容 where 条件

4、读取数据
(1)简单读取,查询所有列(*) 所有行(没有加条件)
select * from Info
(2)读取特定列
select code,name from Info
(3)条件查询
select * from Info where code='p003'
(4)多条件查询
select * from Info where code='p003' or nation='n002' #或的关系
select * from Info where sex=0 and nation='n002' #与的关系
(5)关键字查询(模糊查询)
查所有包含奥迪的汽车
select * from car where name like '%奥迪%'; #百分号%代表任意多个字符
查以'皇冠'开头的所有汽车
select * from car where name like '皇冠%';
查询汽车名称中第二个字符是'马'的
select * from car where name like '_马%'; #下划线_代表任意一个字符
(6)排序查询
select * from car order by powers #默认升序排列
select * from car order by powers desc #升序asc 降序 desc
先按brand升序排,再按照price降序排
select * from car order by brand,price desc


(7)范围查询
select * from car where price>40 and price<60
select * from car where price between 40 and 60

(8)离散查询
select * from car where price=30 or price=40 or price=50 or price=60;
select * from car where price in(30,40,50,60)
select * from car where price not in(30,40,50,60)

(9)聚合函数(统计查询)
select count(*) from car
select count(code) from car #取所有的数据条数
select sum(price) from car #求价格总和
select avg(price) from car #求价格的平均值
select max(price) from car #求最大值
select min(price) from car #求最小值

(10)分页查询
select * from car limit 0,10 #分页查询,跳过几条数据(0)取几条(10)
规定一个每页显示的条数:m
当前页数:n
select * from car limit (n-1)*m,m

(11)去重查询
select distinct brand from car

(12)分组查询
查询汽车表中,每个系列下汽车的数量
select brand,count(*) from car group by brand
分组之后,只能查询该列或聚合函数

取该系列价格平均值大于40的系列代号
select brand from car group by brand having avg(price)>40

取该系列油耗最大值大于8的系列代号
select brand from car group by brand having max(oil)>8


高级查询:
1.连接查询
select * from Info,Nation
形成笛卡尔积
select * from Info,Nation where Info.nation=Nation.code

select Info.code,Info.name,Info.sex,Nation.name as '民族',Info.birthday from Info,Nation where Info.nation=Nation.code

select * from Info join Nation on Info.nation=Nation.code

2.联合查询
select code,name from Info
union
select code,name from Nation

3.子查询
子查询查询的结果作为父查询的条件

(1)无关子查询:子查询执行的时候和父查询没有关系
查民族为'汉族'的所有学生信息
select * from Info where nation=(select code from nation where name='汉族')

查询生产厂商为'一汽大众'的所有汽车信息
select * from car where brand=()
select brand_code from brand where prod_code=()
select prod_code from productor where prod_name='一汽大众'


select * from car where brand in(select brand_code from brand where prod_code=(select prod_code from productor where prod_name='一汽大众'))

(2)相关子查询
子查询在执行的时候需要用到父查询的内容

查询汽车表中,汽车油耗小于该系列平均油耗的所有汽车信息

select * from car where oil<(该系列平均油耗)
select avg(oil) from car where brand =(该系列)

select * from car a where oil<(select avg(oil) from car b where b.brand =a.brand)
1.注释语法:--,#
2.后缀是.sql的文件是数据库查询文件
3.保存查询
4.在数据库里面 列有个名字叫字段 行有个名字叫记录

CRUD操作:
create 创建(添加)
read 读取
update 修改
delete 删除

1、添加数据
insert into Info values('p009','张三',1,'n001','2016-8-30 12:9:8') ;
给特定的列添加数据
insert into Info (code,name) values('p010','李四');
自增长列的处理
insert into family values('','p001','数据','T001','数据',1);

insert into 表名 values(值)

2、删除数据
删除所有数据
delete from family
删除特定的数据
delete from Info where code='p001'

delete from 表名 where 条件

3、修改数据
修改所有数据
update Info set name='徐业鹏'
修改特定数据
update Info set name='吕永乐' where code='p002'
修改多列
update Info set name='吕永乐',sex=1 where code='p003'

update 表名 set 要修改的内容 where 条件

4、读取数据
(1)简单读取,查询所有列(*) 所有行(没有加条件)
select * from Info
(2)读取特定列
select code,name from Info
(3)条件查询
select * from Info where code='p003'
(4)多条件查询
select * from Info where code='p003' or nation='n002' #或的关系
select * from Info where sex=0 and nation='n002' #与的关系
(5)关键字查询(模糊查询)
查所有包含奥迪的汽车
select * from car where name like '%奥迪%'; #百分号%代表任意多个字符
查以'皇冠'开头的所有汽车
select * from car where name like '皇冠%';
查询汽车名称中第二个字符是'马'的
select * from car where name like '_马%'; #下划线_代表任意一个字符
(6)排序查询
select * from car order by powers #默认升序排列
select * from car order by powers desc #升序asc 降序 desc
先按brand升序排,再按照price降序排
select * from car order by brand,price desc

原文地址:https://www.cnblogs.com/little-rock/p/7599992.html