SQL数据库防挂科

本文详细介绍了SQL的相关操作,包括模式的创建与删除、表的定义、修改和删除,以及索引的管理。文章还深入讲解了SQL查询语法,如选择、去重、条件查询、聚合函数、分组和连接查询。此外,提到了数据库的安全性和完整性概念,这些都是数据库管理的基础知识。
摘要由CSDN通过智能技术生成

注:本篇文章的图片等内容来自B站UP主:编程张无忌

一、绪论

 

  

二、关系数据库

A-B,把A中的B部分都去掉

 

 

 

 

 

 

 

 

 

三、SQL上

1、模式的定义和删除

单纯定义一个模式:  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(是否强制删除子部分)

2、表的定义/删除/修改

alter table test

add email varchar(255);

3.8  alter table Student

add S_entrance DATE;

3.9  alter table Student 

alter column Sage int;

  3、索引的建立/删除/修改

 create unique/cluster index  Stu_sno_index on Student(Sno);

alter index SCno rename to SCSno;

drop index Stusname; 

四、SQL中

  1、查询所有/部分列

 

select * from Student;  等价于

selectSno,Sname,Ssex,Sage,Sdept from Student; 

2、起别名

 as后面可以起别名,且as可以省略

select id 标志,title 课程名字 from edu_course;

select id as 标志,title as 课程名字 from edu_course;

 3、结果去重

select title 课程名字 from edu_course;

select distinct title 课程名字 from edu_course;

4、查询结果加条件 

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;

5、聚集函数的使用 

 6、分组查询group by

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

7、连接

之前说过,两个表中的同名列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;

8、多表查询

 select * from edu_course ec,edu_course_description ecd,edu_chapter ech 

where ec.id=ecd.id  and  ec.id=ech.id;

9、嵌套查询

 select * from edu_course ec where teacher_id in

(select * from edu_course where teacher_id=10 or teacher_id=0);

10、带有any/all的子查询

 区别于  in+子查询

 select * from ec where price>ANY

(select price from ec where price<10);

11、带exists的子查询

select Sname from Student

where NOT EXISTS

        select * from Course where NOT EXISTS

        (

                select * from sc where Sno=Student.Sno  and Cno=Course.Cno

        )

12、集合查询

 

 union   intersect    except

五、SQL下

 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下

六、数据库安全性

七、数据库的完整性


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值