标签:scores let not 成绩 gen sql 账号 varchar def
练习:账号信息表,用户组,主机表,主机组
#用户表
create table user(
id int not null unique auto_increment,
username varchar(20) not null,
password varchar(50) not null,
primary key(username,password)
);
#用户组表
create table usergroup(
id int primary key auto_increment,
groupname varchar(20) not null unique
);
#主机表
create table host(
id int primary key auto_increment,
ip char(15) not null unique default ‘127.0.0.1‘
);
#业务线表
create table business(
id int primary key auto_increment,
business varchar(20) not null unique
);
#建关系:user与usergroup
create table user2usergroup(
id int not null unique auto_increment,
user_id int not null,
group_id int not null,
primary key(user_id,group_id),
foreign key(user_id) references user(id),
foreign key(group_id) references usergroup(id)
);
#建关系:host与business
create table host2business(
id int not null unique auto_increment,
host_id int not null,
business_id int not null,
primary key(host_id,business_id),
foreign key(host_id) references host(id),
foreign key(business_id) references business(id)
);
#建关系:user与host
create table user2host(
id int not null unique auto_increment,
user_id int not null,
host_id int not null,
primary key(user_id,host_id),
foreign key(user_id) references user(id),
foreign key(host_id) references host(id)
);
mysql> use day46 Database changed mysql> create table user( -> id int not null unique auto_increment, -> username varchar(20) not null, -> password varchar(50) not null, -> primary key(username,password) -> ); Query OK, 0 rows affected (0.02 sec) mysql> create table usergroup( -> id int primary key auto_increment, -> groupname varchar(20) not null unique -> ); Query OK, 0 rows affected (0.02 sec) mysql> create table host( -> id int primary key auto_increment, -> ip char(15) not null unique default ‘127.0.0.1‘ -> ); Query OK, 0 rows affected (0.01 sec) mysql> create table business( -> id int primary key auto_increment, -> business varchar(20) not null unique -> ); Query OK, 0 rows affected (0.01 sec) mysql> create table user2usergroup( -> id int not null unique auto_increment, -> user_id int not null, -> group_id int not null, -> primary key(user_id,group_id), -> foreign key(user_id) references user(id), -> foreign key(group_id) references usergroup(id) -> ); Query OK, 0 rows affected (0.01 sec) mysql> create table host2business( -> id int not null unique auto_increment, -> host_id int not null, -> business_id int not null, -> primary key(host_id,business_id), -> foreign key(host_id) references host(id), -> foreign key(business_id) references business(id) -> ); Query OK, 0 rows affected (0.01 sec) mysql> create table user2host( -> id int not null unique auto_increment, -> user_id int not null, -> host_id int not null, -> primary key(user_id,host_id), -> foreign key(user_id) references user(id), -> foreign key(host_id) references host(id) -> ); Query OK, 0 rows affected (0.01 sec) mysql> show tables; +-----------------+ | Tables_in_day46 | +-----------------+ | business | | host | | host2business | | user | | user2host | | user2usergroup | | usergroup | +-----------------+ 7 rows in set (0.00 sec)
作业:
# 班级表
cid caption
# 学生表
sid sname gender class_id
# 老师表
tid tname
# 课程表
cid cname teacher_id
# 成绩表
sid student_id course_id number
#班级表 mysql> create table class( -> cid int primary key auto_increment, -> caption char not null -> ); Query OK, 0 rows affected (0.01 sec) #学生表 mysql> create table student( -> sid int primary key auto_increment, -> sname char(20) not null, -> gender enum(‘male‘,‘female‘)default ‘male‘ -> class_id int , -> foreign key(class_id) references class(id) -> on update cascade -> on delete cascade -> ); Query OK, 0 rows affected (0.01 sec) #老师表 mysql> create table teacher( -> tid int primary key auto_increment, -> tname char(20) not null -> ); Query OK, 0 rows affected (0.01 sec) #课程表 mysql> create table course( -> cid int primary key auto_increment, -> cname char(50), -> teacher_id int , -> foreign key(teacher_id) references teacher(id) -> on update cascade -> on delete cascade -> ); Query OK, 0 rows affected (0.01 sec) #成绩表 mysql> create table scores( -> id int primary key auto_increment, -> student_id int not null, -> course_id int not null, -> number float not null -> ); Query OK, 0 rows affected (0.01 sec) #建成绩关联表 mysql> create table stu2course( -> id int not null, -> stu_id int not null, -> cs_id int not null, -> primary key(stu_id,cs_id), -> foreign key(stu_id) references student(id) -> on update cascade -> on delete cascade, -> foreign key(cs_id) references course(id) -> on update cascade -> on delete cascade -> );
标签:scores let not 成绩 gen sql 账号 varchar def
原文地址:https://www.cnblogs.com/heirenxilou/p/12831370.html