常见Sql语句

创建一个学生表

 create table Student

(  Sno char6primary key/*列级完整性约束条件,Sno是主键*/

     Sname char10unique/*列级完整性约束条件,Sname唯一*/

     Ssex char2check (Ssex in'','')),

     Sage smallint,

     Sdate date not null 



);

数据表的修改

   eg.   向Student表中添加所在系的列

        

 alter table Student add Sdept char20);

    eg. 将年龄的数据类型该文长整型

alter table Student alter column Sage int;

数据表的删除 

    (基本表删除后,表中的数据以及此表上建立的索引,视图,触发器等自动被删除,所以要慎用删除)

drop table Student;

索引的建立(加快查询速度,提高效率)和删除

     为Student表的学号按升序建立索引   为student表的学号按升序,成绩按降序建立唯一的索引

 create unique index Stusno on Student(Sno); /*不说明的话,默认为升序*/
 Create unique index Stusnoandscore on Student(Sno asc,Grade desc);

     索引的删除

     删除student表的索引

drop index Stusno;

    查询:

    查询Student所有的信息

    查询姓名,和年龄

select * from Student;
select sname,sage from Student;

    建立个SC的表 有学号Sno 课程号 Cno 成绩Grade

    在这个SC表中查询所有选了课程的学生的学号,并消除重复的学号

select distinct Sno from SC;

    查询计算机系的所有学生的学号和姓名

 select Sno,Sname from Student where Sdept ='CS';

    查询年龄在20-30岁之间的学生的姓名

select Sname,Sdept,Sage from Student where Sage between 20 and 30;

     字符串匹配  

    找出姓名中姓为张名里有伟的学生的姓名和性别

 select sno,sname from Student where Sname like '张_  _伟%';

     

     exists: 

select Sneme from student where exists(select * from SC where Sno=Student.Sno);

      

 

原文地址:https://www.cnblogs.com/2714585551summer/p/5904360.html