create table sc
( sno char(9) ,
  cno char(4),
  grade smallint,
  primary key(sno,cno),
  foreign key (sno) references student(sno),
  foreign key (cno) references course(cno)
);
1.修改基本表:
 alter table <表名>
  [add <新列名> <数据类型> [完整约束]];//新加一个字段
  [drop <完整性约束名>];//删除指定的约束条件
  [alter columm <列名><数据类型>];//修改指定列的名称和数据类型;
eg:
  alter table student add s_entrance date;
alter table student alter column sage int;//????
  alter table course add unique(cname);
2.删除基本表
  drop table<表名>[restrict] [cascade]//restrict 有限制的删 cascade 将和表有关的视图等一起删掉;
eg:
  drop table student;
3.索引的建立和删除;
  create [unique]  [cluster] index <索引名> on <表名> (<列名>
  [ [<次序>] [,<列名> [<次序>]]……);
     unique 表明次索引的每一个索引值只对应唯一的数据记录
     cluster 表示要建立的索引是聚族索引
     表名 是要基于哪个表建立的索引
     列名 指那哪一列;
     次序 可以是asc(升序) desc(降序)
eg: create cluster index stuname on student (sname asc);
    create unique index stuno on student (sno);//默认升序
    create unique index coursno on course (cno);
    create unique index scno on sc(sno asc, cno desc);
  删除索引
   drop index <索引名>
eg: drop index stusname;
4.数据查询
  select [all|distinct] <目标列表达式> [,<目标表达式>]……
  from <表名或视图名> [,<表名或视图名>]…………
  [where <条件表达式>]
  [group by <列名1> [having <条件表达式>]]
  [order by <列名2> [asc|desc]];
  单表查询
   select sno sname from student;
   select * from student //查询全部内容
   查询经过计算的值
 select 子句的<目标表达式> 不仅可以是表中的属性列,也可以是表达式
eg : select sname,2004-sage from student
     select sname,'year of birth:',2004-sage,lower(sdept) from      student
  用指定别名来改变查询结果的列标题 即别名
eg: select sname NAME,'year of birth:'BIRTH,2004-sage     BIRTHDAY,lower(sdept) DEPATMENT from student; 
   消除相同的行 用distinct
eg: select distinct sno from sc;
查询满足条件的元组
 条件:
   比较:= ,>,<,>=,<=,<>,!>,<!,not+上述比较条件
   确定范围:between ,and ,not, between and;
   确定集合:in ,not in;
   字符匹配  like ,not like;
   空值      is null,is not null;
   多重条件(逻辑运算) and ,or,not;
eg:select sname from student where sdept='cs';
   select sname,sage from student where sage<20;
   select distinct sno from sc where grage<60;
确定范围
eg:select sname,sdept,sage from student where asge between 20    and 23;
    select sname,sdept,sage from student where sage not between     20 and 23;
确定集合
eg;  select sname,ssex from student where sdept in     ('cs','ma','is');
     select sname,ssex from student where sdept not in     ('cs','ma','is');
字符匹配
      [not] like '<匹配>' [escape '<换码字符>']
     like %(任意长度) _(任意一个);
eg:select * from student where sno like '20092208';
    等价于:select * from student where sno='20092208';
   查询姓刘的:
   select sname,sno,ssex from student sname like '刘%';
   select sname,sno,ssex from student sname like '刘_';
   查询名为明的:
   select sname,sno,ssex from student snane like '_明';
   select sname,sno,ssex from student sname like '%明';
如果用户查询的字符本身就含有通配符 这时就要用escape'<换吗字符>'
eg:select cno,credit from coures where cname like 'DB\_Design' 
   escape '\';//这样\_就等于_;
   select * from course where cname like 'DB\_%i__' escape '\';
空值的查询:
  select sno,cno from sc where grade is null;
  select sno,cno from sc where grage id not null;
多重条件查询:and or(and 优先级高于or)
eg:select sname from student where sdept='cs' and sage<20;
   select sname,ssex from student where sdept='cs' or sdept='is';
5.order by 子句的使用
   用户可以用order by 子句对查询结果按照一个或多个属性的升序(asc  )或降序(desc) 默认为升序
 eg:
   select sno,grage from sc where cno='3' order by grade desc;
   select * from student order by dept,sage desc;
6.聚集函数
   count([distinct|all]*)       统计元组个数
   count([distinct|all]<列名>)  统计一列中的值的个数
   sum([distinct|all]<列名>)    计算一列值的总和
   avg([distinct|all]<列名>)    计算一列值的平均值
   max([distinct|all]<列名>)    求一列中的最大值
   min([distinct|all]<列名>)     求一列中的最小值
  默认为all
eg: select count(*) from student;//查询学生的总人数
    select count(distinct sno) from sc;
    select avg(grade) from sc where cno='1';
    select max(grade) from sc where cno='1';
    select sum(credit) from sc,course where sno='222222'and     sc.cno=course.cno;
7.group by子句
   group by 子句将查询结果按某一列或多列的值分组,值相等的为一组
   对查询结果分组的目的是为了细化聚集函数的作用对象
eg:select cno,count(sno) from sc group by cno;
   该句对查询结果按cno的值分组,具有相同的cno的值的元组为一组然后对每一组使用count函数 以求改组的学生人数;如果分组后 还要按一定的条件 可以使用having 来指定条件
eg: select sno from sc group by sno having count(*)>3;
8.多表查询
  [<表名1>,] <列名> <比较运算符>[<表名2>,] <列名2>
  = ,>,<,>=,<=,<>,!>,<!
等值与非等值连接查询
eg:select student.* ,sc.* from student,sc where     student.sno=sc.sno;
表的自身连接
   先要给表取别名
eg:select first.cno,second.cpno from course firsr,course second
   where firs.cpno=second.cno;
外连接
  主表的内容全部列出//左右外连接
eg:select student sno,sname,sage,sdept,cno,grade from student
   left out join sc on (student.sno=sc,sno)//可用using 去掉重复值
复合条件连接
eg:select student.sno,sname,from student,sc where student.sno=sc.
   sno and sc.cno='2' and sc.grade>90;
   select student.sno,sname,cname,grade from student,sc,course 
   where student.sno=sc.sno and sc.cno=course.cno;
嵌套查询 多个select-from-where
eg:select sname from student where sno in(select sno from sc    where cno='2';
: 查询与流程在同一个系的学生
   select sno,sname,sdept from student where sdept=(select sdept
   from student dname='流程');
查询选修了课程名为“信息系统”的学生号和姓名
  select sno,sname from student where sno in(select sno from sc     where cno in(select cno from course where cname='信息系统')
找出每个学生超过他选修课程的平均的课程号
  select sno,cno from sc x where grade>=(select avg(grade) from
  sc y where y.sno=x.sno);
9.带有any(some) 或all谓词的子查询 如> ,>=,<,<=
eg:查询其他系统中比计算机科学系某一学生年龄小的学生姓名和年龄
  select sname,sage from student where sage<any(select sage
  from student where sdepto='cs');
 查询其他系统中比计算机科学系所有学生年龄小的学生姓名和年龄
eg:select sname,sage from student where sage<all(select sage    
    from student where sdept='cs');
10.带有exists谓词的子查询
   只产生逻辑真 true 或逻辑假false
eg:select sname from student where exists(select * from sc
     where sno=student.sno and cno='1');
   select sname from student where not exists(select * from sc
     where sno= student.sno and cno='1');
11.集合查询
   union 并操作,intersect 交操作 except差操作
   select * from student where sdept='cs' union select * from    student where sage<=19;
 eg:select * from student where sdept='cs' intersect select *     from student where sage<=19;
   查询计算机科学系的学生与年龄不大于19的学生的差集
eg:select * from student where sdept='cs' except select student
    where sage<=19;
总结:select 语句的一般格式
   select [all|distinct] <目标表达式>[别名][,<目标表达式>[别名]…… from <表名或视图名>[别名][,<表名或视图名> [别名]]……
  [where <条件表达式>]
  [group by <列名>[having <条件表达式>
  [order by <别名>[asc|all];
数据更新
 1.插入数据
 插入元组
 insert into <表名>[(<属性列> [,<属性列>……]
  values(<常量1> [,<常量2>]…………);
eg:insert into student (sno,sname,sdept,sage)
    values('2002','程灿明','男','is','18');
  inert into student values('222','你好','男',12,'cs');
  inert into student values('222','你好','男',12,null);
插入子查询结果
   intsert into <表名>[(<属性列1> [,<属性列2>……)]
   insert into deptage(sdept,avg) select sdept,avg(sage)
修改数据
   update<表名> set <列名>=<表达式> [,<列名>=<表达式>]……
   [where <条件>];
eg:update student set sage=22 where sno='2222';
   将所有学生的年龄增加1岁
   update student set sage=sage+1;
   update sc set grade=0 where 'cs'=(select sdapt from
    student where student,sno=sc,sno);
删除数据
   delete from <表名> [where <条件>];
eg: delete from student where sno='222';
    delete from sc//删除所有记录
    delete from sc where 'cs'=(select sdept from student where     student.sno=sc.cno);
12.视图
   
   from student group bu sdept;
13.视图
  create view <视图名> [<列名> [,<列名>]……)]
   as <子查询>
   [with check option];//进行update insert delete操作
  eg:
   create view is_student
   as
   select sno,sname,sage
   from student
   where sdept='is';
 
 
 
                     
            
        













 
                    

 
                 
                    