Create a database table small practice

1, finishing blog
2, create a stu tables, fields are: auto-increment primary key id, the name is not empty, the default value sex (enumerated types), unlimited height
create table stu(
id int primary key auto_increment,
name varchar(16) not null,
gender enum('male', 'female', 'wasai') default 'wasai',
height float
);
3, insert the following three data table sequentially stu

i) comprises inserting a data id, name, gender, height information of the four

ii) inserting a name, data gender, height information of the three

iii) an insertion of only the data 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, to achieve a new copy of the existing table new_stu stu table fields, data constraints and

create table new_stu like stu;
insert into new_stu select * from stu;
5, create a card with the name, age, teacher's table, add at the end of the salary field, add the id primary key field after his name

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, thinking: 5 id field will move to the forefront of the table, the final order of the fields id, name, age, salary

alter table teacher modify id int first;
7, complete the creation of many-to-table relationship between citizens and the State table table

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, students create a complete curriculum of many to many tables and table relationships

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 a one to one relationship table complete with table of authors table (thinking why this design)

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

 

Guess you like

Origin www.cnblogs.com/1832921tongjieducn/p/11069190.html