网站建设思维导图婚庆公司合同模板
(一)基础
创建数据库,数据库名为shujuku01
- SQL 对大小写不敏感:SELECT 与 select 是相同的
 - create database shujuku01
 - 创建表,if not exists表示如果表不存在则创建,存在则不创建
 - create database if not exists shujuku01;

 
创建表,表名为biao01
create table biao01(
 biaoid int not null primary key,
 name varchar(30) not null,
 money varchar(30) not null,
 waijianid int not null
 )
- not null 不为空约束
 - primary key 主键约束
 - unique唯一约束,确保一列的每一行必须具有唯一值
 - default默认约束
 - foreign key 外键约束
 - check检查约束
 - AUTO_INCREMENT 自增

 
insert into插入表
insert into biao01(biaoid,name,money,waijianid)value(2,“二号”,102,2);
 
显示表所有列的信息
- describe biao01;

 
select 查询表数据
- select * from biao01 查询biao01 表所有信息

 - select biaoid,name,money from biao01 查询表部分信息

 
where过滤
- where子句允许运算符:=,<,>,>= <=,like,in,not in,between…and…
 - in运算符是逻辑运算符,用于检查一组值中是否存在特定值



 - IN运算符是逻辑运算符,用于检查一组值中是否存在特定值,in(100,300)检查money是否存在100或者300这两个特定的值。


 
and 和or

 
order by子句用于按升序或降序对查询返回的数据进行排序
- asc升序
 - desc倒序




 
TOP子句用于限制返回的行数,一些数据库不支持top支持limit
- select top 3 * from biao01
 - select * from biao01 limit 3

 
distinct子句用于从结果集中删除重复的行,查询的时候不显示重复数据

 
update 更新已存在数据

 
delete 按条件删除数据,可以删除所有数据

 
- delete from biao01;#删除表中所有数据
 
truncate 清空表
- truncate table biao01;
 
drop 删除表,可一次性删除多个
- drop table biao01,biao02;
 
(二)SQL链接
前期准备:一个教师表,一个学生表,一个班级表。
 教师表:五个老师:wang01-wang05
 学生表:八个学生:sthdent01-08
 班级表:三个班级 class01-03
 相关代码如下:
create table if not exists teacher(tid int not null primary key auto_increment,tmane varchar(30),tmoney varchar(30),tclassid int 
);
create table if not exists student(sid int not null primary key auto_increment,sname varchar(30),sclassid int
);
create table if not exists class(cid int not null primary key,cname varchar(30)
)insert into teacher(tid,tname,tmoney,tclassid)value(101,"wang01",1000,301);
insert into teacher(tid,tname,tmoney,tclassid)value(102,"wang02",2000,302);
insert into teacher(tid,tname,tmoney,tclassid)value(103,"wang03",3000,303);
insert into teacher(tid,tname,tmoney,tclassid)value(104,"wang04",4000,304);
insert into teacher(tid,tname,tmoney,tclassid)value(105,"wang05",5000,305);insert into student(sid,sname,sclassid)value(201,"sthdent01",301);
insert into student(sid,sname,sclassid)value(202,"sthdent02",301);
insert into student(sid,sname,sclassid)value(203,"sthdent03",301);
insert into student(sid,sname,sclassid)value(204,"sthdent04",301);
insert into student(sid,sname,sclassid)value(205,"sthdent05",301);
insert into student(sid,sname,sclassid)value(206,"sthdent06",301);
insert into student(sid,sname)value(207,"sthdent07");
insert into student(sid,sname,sclassid)value(208,"sthdent08",302);insert into class(cid,cname)value(301,"class01");
insert into class(cid,cname)value(302,"class02");
insert into class(cid,cname)value(303,"class03"); 
- join是用来链接两个表的
 - inner join 内连接

 - 外连接: 左连接left join

 - 外连接: 右链接right join

 - 交叉链接:cross join

 - 外连接: 完全链接full join。MySQL不支持全连接,但是Oracle支持。
 - MySQL实现全连接,需要使用关键字"union"或者"union all",前者可以去重,后者不去重
 - union和union all使用时,select下的字段数量必须一致,否则会报错
 
select * from teacher 
union all
select * from student 
