//1-2 (修改数据库表) 在Student表中增加SSEX(C,2) 字段 alter table student add ssex char(2); //1-3(修改数据库表) 将Student表中把Sname 字段修改为Sname(C,10)且为非空。 alter table student modify sname char(10) not null; //mysql alter table student alter column sname char(10) not null; //sqlServer //1-4(建立索引) 为Score表按学号升序和课程号降序分别建立索引,索引名分别为 //SC_NDXSNO和SC_NDXCNO。为Score表按分数降序建立索引,索引名为GRADE_NDX。 create index sc_ndxsno on score(sno asc); create index sc_ndxcno on score(cno desc); create index GRADE_NDX on score(score desc); //1-5 (删除索引) 删除索引SC_NDX_SNO。 drop index sc_ndxsno on score; //mysql drop index score.sc_ndxsno ; //sqlServer
//-----插入数据---- //2-1 (插入数据) 按前面各表中的数据分别插入到教学数据库的四个数据库表中。 //**学生表** INSERT INTO STUDENT VALUES('96001','马小燕','CS','01',21,'女'); INSERT INTO STUDENT VALUES('96002','黎明','CS','01',18,'男'); INSERT INTO STUDENT VALUES('96003','刘东明','MA','01',18,'男'); INSERT INTO STUDENT VALUES('96004','赵志勇','IS','02',20,'男'); INSERT INTO STUDENT VALUES('97001','马蓉','MA','02',19,'女'); INSERT INTO STUDENT VALUES('97002','李成功','CS','01',20,'男'); INSERT INTO STUDENT VALUES('97003','黎明','IS','03',19,'女'); INSERT INTO STUDENT VALUES('97004','李丽','CS','02',19,'女'); INSERT INTO STUDENT VALUES('96005','司马志明','CS','02',18,'男'); //**课程表** INSERT INTO COURSE VALUES('001','数学分析',144); INSERT INTO COURSE VALUES('002','普通物理',144); INSERT INTO COURSE VALUES('003','微机原理',80); INSERT INTO COURSE VALUES('004','数据结构',72); INSERT INTO COURSE VALUES('005','操作系统',80); INSERT INTO COURSE VALUES('006','数据库原理',80); INSERT INTO COURSE VALUES('007','编译原理',60); INSERT INTO COURSE VALUES('008','程序设计',40); //**授课表** INSERT INTO Teach VALUES('王成刚','男','004','1999.9.5','CS'); INSERT INTO Teach VALUES('李正科','男','003','1999.9.5','CS'); INSERT INTO Teach VALUES('严敏','女','001','1999.9.5','MA'); INSERT INTO Teach VALUES('赵高','男','004','1999.9.5','IS'); INSERT INTO Teach VALUES('李正科','男','003','2000.2.3','MA'); INSERT INTO Teach VALUES('刘玉兰','女','006','2000.2.3','CS'); INSERT INTO Teach VALUES('王成刚','男','004','2000.2.3','IS'); INSERT INTO Teach VALUES('马悦','女','008','2000.2.3','CS'); //**成绩表** INSERT INTO SCORE VALUES('96001','001',77.5); INSERT INTO SCORE VALUES('96001','003',89); INSERT INTO SCORE VALUES('96001','004',86); INSERT INTO SCORE VALUES('96001','005',82); INSERT INTO SCORE VALUES('96002','001',88); INSERT INTO SCORE VALUES('96002','003',92.5); INSERT INTO SCORE VALUES('96002','006',90); INSERT INTO SCORE VALUES('96005','004',92); INSERT INTO SCORE VALUES('96005','005',90); INSERT INTO SCORE VALUES('96005','006',89); INSERT INTO SCORE VALUES('96005','007',76); INSERT INTO SCORE VALUES('96003','001',69); INSERT INTO SCORE VALUES('97001','001',96); INSERT INTO SCORE VALUES('97001','008',95); INSERT INTO SCORE VALUES('96004','001',87); INSERT INTO SCORE VALUES('96003','003',91); INSERT INTO SCORE VALUES('97002','003',91); INSERT INTO SCORE(SNO,CNO)VALUES('97002','004'); INSERT INTO SCORE VALUES('97002','004',92); INSERT INTO SCORE VALUES('97004','005',90); INSERT INTO SCORE VALUES('97004','006',85); //2-2 (多行插入) 将表Student中在计算机系(‘CS’)的学生数据插入到表S1中。 create table S1 select * from student where SDEPT='CS'; //mysql select * into s1 from student where SDEPT='CS'; //sqlServer //2-4 (修改数据) 将S1表中所有学生的年龄加2。 update s1 set sage=sage+2; //2-5(修改数据) 将Course表中‘程序设计’课时数修改成与‘数据结构’的课时数相同。 UPDATE Course SET CTIME = (SELECT a.ctime from(SELECT CTIME FROM Course WHERE CNAME='数据结构') as a ) WHERE CNAME='程序设计'; //2-6(插入数据) 向Score表中插入数据(‘98001’, ‘001’, 95),根据返回信息解释其原因。 INSERT INTO Score VALUES('98001','001','95'); //原因:违反完整约束条件,未找到父项关键字(主表里面没有98001) //2-7(插入数据) 向Score表中插入数据(‘97001’, ‘010’, 80),根据返回信息解释其原因。 INSERT INTO Score VALUES('97001','010','80'); //-- 原因:违反唯一约束条件(97001已存在) //2-8(删除数据) 删除Score表中学号为‘96001’的成绩信息,根据返回信息解释其原因 DELETE SCORE WHERE SNO='96001'; //-- 原因:违反完整约束条件,已找到子记录(没有删除子表记录的情况下,删除主表记录,触发了完整性约束错误。)