数据库创建表小练习

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