实验三:创建及管理数据表
1、创建表
依据数据表的结构创建相对应的数据表,表结构如下所示; 学生信息表(student )
CREATE (sno char (9) PRIMARY KEY , sname char (8) NOT NULL, ssex char
(2), sage int ,
sdept varchar (20) )
课程信息表(course )
CREATE (cno char (4) PRIMARY KEY , cname varchar (20) NOT NULL, cpno char (4), ccredit int )
选课信息表(sc )
CREATE TABLE sc
(sno char(9),
cno char(4),
grade int,
Constraint PK_sno PRIMARY KEY(sno,cno))
2.修改表结构
1)在表student中增加新字段“班级名称(sclass)”字符类型为varchar(10);
use student
ALTER TABLE student
Add sclass nvarchar(10)
2)在表student中删除字段“班级名称(sclass)”;
use student
ALTER TABLE student
Drop column sclass
3)修改表student中字段名为“sname”的字段长度由原来的6改为8;
use student
ALTER TABLE student
ALTER COLUMN sname char(8)
4)修改表student中ssex字段默认值为‘男’;
use student
ALTER TABLE student
add default'男'for ssex
5)修改表course中cname字段为强制唯一性字段;
use student
ALTER TABLE course
Add constraint ix_course unique (cname)
6)修改表sc中grade字段的值域为0-100;
use student
ALTER TABLE sc
Add constraint CK_grade CHECK(grade between 0 and 100)
7)删除数据表course的唯一性约束;
use student
ALTER TABLE course
drop ix_course。