sql的基本语法(一)

 insert into 学生表(姓名,年龄)values('牛从',22)
insert into 学生表(姓名,年龄,班级,性别)values('田冬冬',22,'四班','男');
insert into 学生表(姓名,年龄,班级,性别)values('崔林',22,'四班','男')

创建模式
create schema "s-t" authorization wang

create schema test authorization zhang
  create table tab1(col1 smallint,
                    col2 int,
                    col3 char(20),
                    col4 numeric(10,3),
                    col5 decimal(5,2)
删除模式                   )
drop schema zhang cascade
//cascade是级联的意思,指也删除了zhang 模式下的所有数据包括tab1

创建表
create table yiyi
 (Sno char(9) primary key,
   Sname char(20) unique, /* Sname 取唯一值*/
   Ssex char(2),
   Sage smallint,
   Sdept char(20)
 );

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)
  );

create table course
 ( Cno char(4) primary key ,
   Cname char(40),
    Credit smallint,
    Foreign key(Cno) references Course(Cno)
 );
 
create table "s-t".student
       (name char(7),
        age  smallint,
        sex  char(2)
       ); 

增加列
alter table student add S_entrance datetime

修改列
alter table Student alter column Sage int
将Sage列类型改为int型

增加列中的约束条件
alter table Course add unique(Cname)

删除表
DROP TABLE  Student

创建视图
create  view is_student
As
 select Sno,Sname,Sage
 from student
 where sdept="is"
 
创建索引
CREATE  INDEX Stusname
           ON   Student(Sname)
--在student表的sname(姓名)列上建立一个聚簇索引

 create unique index stusno on student(sno);

 create unique index coucno on course(cno);

 create unique index scno on sc(sno asc,cno desc)
--student表按学号升序建立唯一索引
--course表按课程号升序建立唯一索引
--sc表按学号升序和课号降序建立唯一索引

删除索引
drop index student.stusname

查询表
 select 姓名,学号 from 学生表

  select 姓名,学号,班级 from 学生表 where 性别='女'

select 姓名 name ,年龄 age ,性别 sex from 学生表

select distinct 性别 from 学生表
--distinct:去掉表中重复行

select distinct sno from sc where grade<60

select sname,sdept,sage from student where sage between 20 and 23


select sname,sdept,sage from student where sage not between 20 and 23

select sname,ssex from student where sdept in('is','ma','cs')

select sname,ssex from student where sdept not in('is','ma','cs')

select * from 学生表 where 学号 like 200313
select 姓名, 学号,性别, 年龄 from 学生表 where 姓名 like '%丽伟'

select * from 学生表 where 姓名 like '崔%'
select  * from 学生表 where 姓名 like '%同%'

select *  from 学生表 where 姓名 not like '%雷%'

select sno,cno from sc where grade is null
--is null 或 is not null
--"is"不能用"="代替

select sno,cno from sc where grade is not null
--查询缺少成绩的学生的学号和相应的课程号

select sname from student where sdept='cs' and sage<20


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值