学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
学号,姓名,性别,年龄,所在系Sno为主键
课程表:Course (Cno, Cname,)
课程号,课程名Cno为主键
学生选课表:SC (Sno, Cno, Score)
学号,课程号,成绩Sno,Cno为主键
1.用SQL语句创建学生表Student,定义主键,姓名不能重名,性别只能输入男或女,所在系的默认值是 “计算机”
2.修改Student表中年龄(Sage)字段属性,数据类型由int改变为smallint
3.为SC表建立按学号(Sno)和课程号(Cno)组合的升序的主键索引,索引名为SC_INDEX
4.创建一视图Stu_info,查询全体学生的姓名,性别,课程名,成绩
①登录MySQL并切换数据库
②创建Student表
mysql> create table Student (
-> Sno int(20) not null primary key,
-> Sname varchar(25) not null,
-> Ssex char(3),
-> Sage int,
-> Sdept varchar(30)
-> );
Query OK, 0 rows affected, 1 warning (0.01 sec)
③ 创建Course表
mysql> create table Course (
-> Cno varchar(25) primary key,
-> Cname varchar(25) not null
-> );
Query OK, 0 rows affected (0.01 sec)
④创建SC表
mysql> create table SC (
-> Sno int(20),
-> Cno varchar(25),
-> Score int(10),
-> primary key(Sno,Cno),
-> foreign key(Sno) references Student(Sno),
-> foreign key(Cno) references Course(Cno)
-> );
Query OK, 0 rows affected, 2 warnings (0.02 sec)
①命令
mysql> create table Student (
-> Sno int(20) primary key comment '学号',
-> Sname varchar(25) unique comment '姓名',
-> Ssex char(3) check (Ssex in ('男','女')),
-> Sage int,
-> Sdept varchar(30) default '计算机'
-> );
Query OK, 0 rows affected, 1 warning (0.01 sec)
②结果
①命令
mysql> alter table Student modify Sage smallint;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
②结果
①命令
mysql> create table SC (
-> Sno int(20),
-> Cno varchar(25),
-> Score int(10),
-> primary key(Sno,Cno),
-> foreign key(Sno) references Student(Sno),
-> foreign key(Cno) references Course(Cno)
-> );
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> create index SC_INDEX on SC(Sno asc,Cno asc);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
②结果
①命令
mysql> INSERT INTO `Student` VALUES (1001,'张三','男',21,'计算机');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO `Course` VALUES (1, '英语');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO `SC` VALUES (1001, '1', 25);
Query OK, 1 row affected (0.00 sec)
mysql> select s.Sname,s.Ssex,c.Cname,SC.Score from Student s
-> inner join SC
-> inner join Course c
-> on s.Sno=SC.Sno
-> and SC.Cno=c.Cno;
②结果