mysql数据库试验答案
实验一实验一创建、修改数据库和表结构创建、修改数据库和表结构 1 1、用、用 createcreate 建立教学数据库的五个基本表建立教学数据库的五个基本表 ((1 1)) 学生表(学号,姓名,性别,年龄)学生表(学号,姓名,性别,年龄) ,,studentSnostudentSno,, sname sname,, ssexssex,,sagesage ;; ((2 2)) 课程表课程表 (课程号,(课程号, 课程名,课程名, 学分)学分) ,, CourseCourse Cno,Cno, Cname,Cname, creditcredit;; ((3 3)选课表(学号,课程号,成绩))选课表(学号,课程号,成绩) ,,SC Sno,, Cno, grade SC Sno,, Cno, grade ;; 44 教师表(教师号,姓名,性别,出生年月,系部,职称,地址)教师表(教师号,姓名,性别,出生年月,系部,职称,地址) ,, TTnoTTno,,TnameTname,,ssexssex,,birthdaybirthday,,deptdept,,titletitle,,addressaddress ;; 55 工资表(教师号,基本工资,职务工资,合计)工资表(教师号,基本工资,职务工资,合计) ,,SalaryTnoSalaryTno,, jbgzjbgz,,zwgzzwgz,,hjhj;; CreateCreateDatabaseDatabase StudentStudentdefaultdefault charactercharactersetsetutf8utf8 default COLLATE utf8_bindefault COLLATE utf8_bin; ; Use Student;Use Student; Create Table StudentCreate Table Student SNoSNoc char20 primary key,har20 primary key, SName char20 ,SName char20 , SSexSSexchar4 default char4 default 男男,, SAgeSAgeintint ENGINEInnoDBENGINEInnoDB; ; Create Table CourseCreate Table Course CNoCNoc char20 primary key,har20 primary key, CName char20 NOT NULL,CName char20 NOT NULL, Cascade,Cascade, CReditCReditf floatloat ENGINEInnoDB ENGINEInnoDB; ; Create Table SCCreate Table SC SNoSNoc char20 NOT NULL,har20 NOT NULL, CNoCNoc char20 NOT NULL,har20 NOT NULL, Grade float,Grade float, Primary KeySNo, CNo,Primary KeySNo, CNo, ForeignForeign KeySNoKeySNo ReferencesReferences StudentSNoStudentSNo OnOn DeleteDelete Foreign KeyCNo References CourseCNoForeign KeyCNo References CourseCNo ENGINEInnoDENGINEInnoDB;B; Create Table TCreate Table T TNoTNoc char20 Primary Key,har20 Primary Key, TName char20 NOT NULL,TName char20 NOT NULL, TSexTSexchar4 default char4 default 男男,, birthday DateTime,birthday DateTime, deptdeptchar20,char20, title char20,title char20, address char20address char20 ENGINEInnoDBENGINEInnoDB; ; Create Table SalaryCreate Table Salary TNoTNoc char20 NOT NULL,har20 NOT NULL, jbgzjbgzfloat,float, zwgzzwgzfloat,float, hj float,hj float, CascadeCascade ForeignForeignKeyTNoKeyTNoReferencesReferencesTTNoTTNoOnOnDeleteDelete ENGINEInnoDBENGINEInnoDB; ; 2 2、用、用 alteralter 修改基本表修改基本表 ((1 1)在已存在的学生表)在已存在的学生表 studentstudent 中增加一个中增加一个 sdeptsdept(系)的新的属性(系)的新的属性 列;列; alter table Student add Dept char20; alter table Student add Dept char20; ((2 2)将学生表)将学生表studentstudent 中中 snamesname 属性列的数据类型修改为变长字符串属性列的数据类型修改为变长字符串 varchar10varchar10。。 alter able Studentalter able Student modify modify colum sname varchar10 colum sname varchar10 3 3、建立一个临时表,然后将其删除、建立一个临时表,然后将其删除 Create Table temp Create Table temp ANoANoc char20 NOT NULL,Bhar20 NOT NULL,Bfloat, C char10 float, C char10 Drop table temp Drop table temp 实验二实验二建立与删除索引建立与删除索引 1 1、、用用 create indexcreate index 在学生表在学生表 studentstudent 的学号的学号 snosno 上建立聚簇索引。上建立聚簇索引。 Create Clustered Index SNo_Index On StudentSN