新建产品库mydb6_product,新建3张表如下:
一、employees表
列1:id,整型,主键
列2:name,字符串,最大长度50,不能为空
列3:age,整型
列4:gender,字符串,最大长度10,不能为空,默认值“unknown”
列5:salary,浮点型
二、orders表
列1:id,整型,主键
列2:name,字符串,最大长度100,不能为空
列3:price,浮点型
列4:quantity,整型
列5:category,字符串,最大长度50
三、invoices表
列1:number,整型,主键自增长
列2:order_id,整型,外键关联到orders表的id列
列3:in date:日期型
列4:total_amount:浮点型,要求数据大于0
建立产品库mydb6_product :
命令:create database mydb6_product;
查看建库信息(建立表先进行查看)
命令:show database;
注意:建立完库之后直接进入养成好习惯,并查看
1、建立employees表:
命令: create table employees(
-> id int primary key,
-> name varchar(50) not null,
-> gender char(10) not null default "unkonwn",
-> salary float);
查看表的信息
命令:desc emloyees;
2、建立orders表
命令: create table orders(
-> id int primary key,
-> name varchar(100) not null,
-> price float,
-> quantity int,
查看表的信息
命令:desc orders;
3、建立 invoices表
命令:create table invoices(
-> number int auto_increment,
-> order_id int,
-> in_date date,
-> total_amount float check(total_amount>0),
-> primary key(number)
-> );
查看表的信息
命令:desc invoices;
构建外键约束
命令:alter table invoices add constraint invoices_zl foreign key (order_id) references orders (id);
查看表的信息: