--为用户zhang创建一个模式test,并在其中定义一个表tab1
create schema test authorization zhang
create table tab1(
col1 smallint,
col2 int,
col3 char(20),
col4 numeric(10,3),
col5 decimal(5,2),
);
--删除模式zhang
/*选用时cascade (级联)删除表中所有文件,restrict(限制)
表示如果模式中所有的数据对像时才能执行DROP SCHEMA*/
---------------------------
--建立一个数据表student
create table student
(
sno char(9) primary key,/*列级完整性条件,表示sno是主码*/
sname char(20) unique,/*sname取唯一值*/
ssex char(2),
sage smallint ,
sdept char(20),
);
select sno from dbo.student;
create table course
(
Cno char(4) primary key,/*列级完整性条件,表示cno是主码*/
Cname char(40) not null,/*列级完整性条件* Cname不可以取空值*/
Cpno char(4),
--cpno的含义是先修课
Ccredit smallint,
foreign key(Cpno)references Course(Cno)
--表级完整性约束条件,Cpno是外码,被参照表是course,被参照列是Cno
);
create table SC
(
sno char(9),
Cno char(4),
Grade smallint ,
primary Key (sno,Cno),
--主码由两个属性组成,必须作为表级别完整性进行定义
foreign key (sno) references student(sno),
--表级别完整性约束条件,sno是外码,被参照表是student
foreign key (Cno) references course(Cno)
);
alter table student
add s_etrance date;
--不论基本表中原来是否有数据,新增加的列一律为空值
-----------------
--修改数据类型
alter table student
alter column sage int;/*column是列的意思*/
--增加课程名称必须取唯一值得约束条件
alter table course
add unique(Cname);
-------------------
----删除表
/*cascade:有限制条件
restrict:无限制条件*/
----------------------------------
--删除student表
--drop table student cascade;
建立视图
create view is_student/*创建视图*/
as
select
sno, sname,sage
from
student
where
sdept='is';
--------------------
/*drop table student restrict;
drop table student cascade;
*/
select * from is_student;
-- SQL sever 没有retrict 和cascade 选项
索引的建立与删除
表中的数据量较大时,建立索引可以加快查询的有效手段
索引类型
(1)顺序文件索引(2)B+树索引(3)散列索引(4)位图索引
--建立索引
create unique index stusno on student(sno);
/*
unique 表明此索引的每一个索引值只对应唯一的数据记录
index 索引关键字
*/
create unique index coucno on course(Cno);
create unique index SCno on SC (sno asc ,Cno desc);
/*
cluster:表示建立的是聚簇索引
ASC 升序
desc 降序
*/
----------------------
--修改索引
alter index SCno rename on SCSno;
--删除索引
drop index stusno on student;