三、SQL语句
1.SQL的特点(优点)
(1)综合统一 (2)高度非过程化 (3)面向集合的操作方式 (4)同一种语法结构提供多种形式 (5)语言简洁,易学易用
2.DDL
MBSS 2323
创建 | 删除 | 修改 | |
模式 | √ | √ | |
表 | √ | √ | √ |
视图 | √ | √ | |
索引 | √ | √ | √ |
创建模式:
create schema 模式名 authorization 用户名;
删除模式:
drop schema 模式名<cascade/restrict>;
模式下创建表:
1.create table 模型名.表名(
XXXXXXXXXX
);
2.show search_path;
set search_path to 模式名,PUBLIC;
创建表:
create table student(
sno char(10) primary key,
sname char(20) not null,
ssex char(2) check(ssex in ('男','女'))
);
删除表:
drop table student <cascade/restrict>; //默认为restrict
修改表(两个增加,两个删除,一个修改)
alter table student add column sage int check(sage between 10 and 100);
alter table student add unqiue(sname);
alter table student drop column 列名<cascade/restrict>;
alter table student drop constraint 完整性约束名;
alter table student alter column sname char(25);
创建视图:
create view 视图名 as 查询语句 [with check option];
删除视图:
drop view 视图名 <cascade>;
//创建索引可以加快查询速度,但需要占用一定存储空间。基本表更新时,索引也要进行相应的维护。
//顺序文件的索引、散列索引、位图索引
创建索引:
create unqiue/cluster index 索引名字 on 表名(xx,xx asc/desc,xx...);
删除索引:
drop index 索引名;
修改索引:
alter index 旧索引名 rename to 新索引名;
3.DML语句(增删改查)
增:
insert into student values (xx,xx,xx);
insert into student (sno,sname,ssex) values (xx,xx,xx);
insert into 表名 属性有没有都可以 子查询;
删:
delete from student;
delete from student where sno = '1630090233';
改:
update student set sage=sage+1 where 条件1 or 条件2.....;
查询:
单表查询
多表连接查询:还包括自身和自身连接的
嵌套查询:相关子查询、不相关子查询
基于派生表的查询
集合查询:union intersect except