數據庫創建表小練習


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
);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM