数据库创建表小练习

1、整理博客
2、创建一个stu表,字段有:自增主键id,不为空姓名,默认值性别(枚举类型),无限制身高
create table stu(
id int primary key auto_increment,
name varchar(16) not null,
gender enum('male', 'female', 'wasai') default 'wasai',
height float
);
3、为stu表依次插入以下三条数据

​ i)插入一条包含id,name,gender,height四个信息的数据

​ ii)插入一条name,gender,height三个信息的数据

​ iii)插入一条只有name信息的数据


insert into stu values(1, 'zero', 'male', 180);
insert into stu(name, gender, height) values('engo', 'female', 175);
insert into stu(name) values('tank');
4、实现新表new_stu对已有表stu的字段、约束及数据的拷贝

create table new_stu like stu;
insert into new_stu select * from stu;
5、创建一张有姓名、年龄的teacher表,在最后添加工资字段,在姓名后添加id主键字段

create table teacher(
name char(10),
age int
);
alter table teacher add salary float;
alter table teacher add id int primary key after name;
6、思考:将5中id字段移到到表的最前方,形成最终字段顺序为id、姓名、年龄、工资

alter table teacher modify id int first;
7、完成 公民表 与 国家表 的 多对一 表关系的创建

create table country(
id int primary key auto_increment,
nationality char(10)
);
create table perple(
id int primary key auto_increment,
name varchar(20),
gender enum('男', '女', '未知'),
country_id int,
foreign key(country_id) references country(id)
on update cascade
on delete cascade
)
8、完成 学生表 与 课程表 的 多对多 表关系的创建

create table student(
id int primary key auto_increment,
name varchar(16),
age int
);
create table course(
id int primary key auto_increment,
name varchar(16)
);
create table book_author(
  id int primary key auto_increment,
  student_id int,
  course_id int,
  foreign key(student_id) references student(id)
  on update cascade
  on delete cascade,
  foreign key(course_id) references course(id)
  on update cascade
  on delete cascade
);
9、完成 作者表 与 作者简介表 的 一对一 表关系的创建(思考为什么要这样设计)

create table author_introduce(
id int primary key auto_increment,
info varchar(300)
);
create table wife(
id int primary key auto_increment,
name varchar(16),
author_introduce_id int unique,
foreign key(author_introduce_id) references author_introduce(id)
  on update cascade
  on delete cascade
);

 

转载于:https://www.cnblogs.com/1832921tongjieducn/p/11069190.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值