首页 > 数据库技术 > 详细

数据库创建表小练习

时间:2019-06-22 16:23:52      阅读:220      评论:0      收藏:0      [点我收藏+]
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)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!