单纯定义一个模式: create schema "S-T" authorizationg WANG
定义模式+ 表/视图/授权 任意一个来描述(创建一个table为例)
create schema "Learn" authorizationg xxx
create talble user
(
id int primary key,
age int,
name varchar(255)
);
drop schema xxx CASCADE/RESTRICT cascade/restrict(是否强制删除子部分)
add email varchar(255);
3.8 alter table Student
add S_entrance DATE;
3.9 alter table Student
alter column Sage int;
create unique/cluster index Stu_sno_index on Student(Sno);
alter index SCno rename to SCSno;
drop index Stusname;
select * from Student; 等价于
selectSno,Sname,Ssex,Sage,Sdept from Student;
as后面可以起别名,且as可以省略
select id 标志,title 课程名字 from edu_course;
select id as 标志,title as 课程名字 from edu_course;
select title 课程名字 from edu_course;
select distinct title 课程名字 from edu_course;
select title 课程名字 from edu_course where price>=2;
select title 课程名字 from edu_course where price!=2;
select title 课程名字 from edu_course where price between 0 and 2;
select 1 in (1,2); select 3 in (1,2);
返回 bool ,查询是否在集合中.
select * from edu_course where title like '%速成';
select * from edu_course where title like '_ysql速成';
查询是否为空
select * from user where username is null;
多重条件查询
select * from user where id=3 and password=123;
select teacher_id from edu_course group by teacher_id having teacher_id =1;
(select t_id from ec group by t_id having t_id =1;)
分组查询不能用where,要使用having
之前说过,两个表中的同名列id可以连接起来
单纯的连接两个表,即得到两个表的笛卡尔积
select * from edu_course ec,edu_course_description ecd (两个表的笛卡尔积)
select * from edu_course ec,edu_course_descrtion ecd where ec.id=ecd.id;(id相等的做笛卡尔积)
自身连接
select * from edu_course ec1,edu_course ec2 where ec1.id=ec2.id;
外连接
假设左边的个数11大于右边10的。
左:左边有几个最终有几个11,其中右边没有的,但左边有(应该被舍弃的)的变为NULL
右:右边有几个最终有几个10,不用舍弃。
内连接:10
左(外)连接
外连接条件不能用where,需要用on
select * from edu_course ec left outer join edu_course_des.. ecd on ec.id=ecd.id;
右(外)连接
select * from edu_course ec right outer join edu_course_des.. ecd on ec.id=ecd.id;
内连接
select * from edu_course ec join edu_course_des.. ecd on ec.id = ecd.id;
select * from edu_course ec,edu_course_description ecd,edu_chapter ech
where ec.id=ecd.id and ec.id=ech.id;
select * from edu_course ec where teacher_id in
(select * from edu_course where teacher_id=10 or teacher_id=0);
区别于 in+子查询
select * from ec where price>ANY
(select price from ec where price<10);
select Sname from Student
where NOT EXISTS
(
select * from Course where NOT EXISTS
(
select * from sc where Sno=Student.Sno and Cno=Course.Cno
)
)
union intersect except
insert into stu values (null,'qq',21,'m','lamp99');
update stu set name='kunkun',age=25,sex='secret' where id = '坤坤'
delete from stu whre id=100;
create view is_stu
as
select * from Student where Sdept='IS';
(with option是在写的时候,检查是否满足select条件,避免脏数据)
脏数据:不满足条件的,反而也被添加的表中了
删除
目录
一、绪论
二、关系数据库
三、SQL上
1、模式的定义和删除
2、表的定义/删除/修改
3、索引的建立/删除/修改
四、SQL中
1、查询所有/部分列
2、起别名
3、结果去重
4、查询结果加条件
5、聚集函数的使用
6、分组查询group by
7、连接
8、多表查询
五、SQL下
六、数据库安全性
七、数据库的完整性