MySQL语句测试——数据定义

MySQL语句测试——数据定义

一、模式
/*1.模式的删除操作*/
drop schema zyl;           /*后面加cascade或restrict报错*/

/*2.模式的创建操作*/
create schema zyl;         /*后面加authorizaition报错*/   

二、表格
/*1.表格的创建操作*/
/*(1)创建表格方式一:
    单独create schema之后,双击左边的schema至加粗状态,再create table
*/

create table Student(
Sno char(9) primary key,   /*列级完整性约束条件,Sno是主码*/
Sname char(20) unique,     /*Sname取唯一值*/
Ssex char(2),
Sage smallint,
Sdept char(20)
);

create table Course(
Cno char(9) 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)
/*表级完整性约束条件,Cpno是外码,被参照表是Course,被参照列是Cno*/
);


/*(2)创建表格的方法二:
    create table 模式名.表名;
*/

create table S_T.Student(
Sno char(9) primary key,   /*列级完整性约束条件,Sno是主码*/
Sname char(20) unique,     /*Sname取唯一值*/
Ssex char(2),
Sage smallint,
Sdept char(20)
);

/*(3)创建表格方法三:
    创建模式的同时创建表
    此方法作废,报错
create schema test
create table Student(
Sno char(9) primary key,   /*列级完整性约束条件,Sno是主码*/
Sname char(20) unique,     /*Sname取唯一值*/
Ssex char(2),
Sage smallint,
Sdept char(20)
);
*/

/*2.表格的修改操作*/
/*(1)增加列,无论表中是否已有数据,新增加的列一律为空值
    alter table 表名 add 新列名 数据类型 (完整性约束);
*/
alter table student add S_entrance date;

/*(2)增加表级约束条件
    alter table 表名 add 标记完整性约束;
*/
alter table course add unique(Cname);

/*(3)修改数据类型
    并非书上的 alter table 表名 alter column 列名 数据类型;
          而是 alter table 表名 modify column 列名 数据类型;
*/
alter table student modify column Sage int;

/*(4)删除列,后面加cascade或restrict仍然报错
    alter table 表名 drop 列名;
*/
alter table student drop S_entrance;

/*(5)删除完整性约束条件,报错
    alter table 表名 drop constraint 完整性约束名;
*/

/*3.表格的删除操作*/
/*(1)普通表格删除
    drop table 表名 [cascade/restrict];  
*/
/*①未加cascade时,直接删掉,同时,sc表也被删掉*/
drop table student;
/*②加上cascade或restrict,运行不成功*/
drop table student cascade;

/*(2)带视图的表格删除操作
    drop table 表名 _/restrict/cascade;
*/
create table Student(
Sno char(9) primary key,   /*列级完整性约束条件,Sno是主码*/
Sname char(20) unique,     /*Sname取唯一值*/
Ssex char(2),
Sage smallint,
Sdept char(20)
);


create view IS_Student  /*Student表上建立视图*/
as
select Sno,Sname,Sage
from Student
where Sdept="IS";

drop table student restrict;    /*删除student表成功,IS_Student视图未删除*/
drop view is_student restrict;
drop table student cascade; /*删除student表成功,IS_Student视图未删除*/
drop view is_student cascade;
drop table student;     /*删除student表成功,IS_Student视图未删除*/
drop view is_student;

三、索引
/*1.索引的建立操作
    create unique/cluster index 索引名 on 表名(列名 次序(asc/desc));
*/
/*
索引:在关系数据库中,索引是一种单独的、物理的对数据库表中一列或多列的值进行排序的一种存储结构,
它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单。
索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容。

unique:表明此索引的每一个索引值只对应唯一的数据记录
cluster:表示要建立的索引是聚簇索引(第7章7.5.2)

asc表示升序,desc表示降序,默认值为asc
*/
create unique index Stusno on student(Sno);
create unique index Cousno on Course(Cno);
create unique index SCno on SC(Sno asc,Cno desc);

/*2.索引的修改及删除均需要在表格的基础上操作*/
/*(1)索引的修改(索引重命名)操作
    alter table 表名 rename index 旧索引名 to 新索引名;
*/
alter index SCno rename to SCSno;   /*报错*/
alter table sc rename index SCno to SCSno;  /*执行成功*/

/*(2)索引删除操作
    alter table 表名 drop index 索引名;
*/
create unique index Stusname on student(Sname); 
alter table student drop index Stusname;

猜你喜欢

转载自www.cnblogs.com/zyl905487045/p/9001097.html