MySQL基本语句以及表约束练习
一、新建产品库mydb6_product并使用
create database mydb6_product;
use mydb6_product;
二、新建表emloyees:
列1:id,整型,主键
列2:name,字符串,最大长度50,不能为空
列3:age,整型
列4:gender,字符串,最大长度10,不能为空,默认值“unknow”
列5:salary,浮点数
create table emloyees(
id int primary key,
name varchar(50) not null,
age int,
gender varchar(10) not null default"unknown",
salary float(5,2)
);
desc emloyees;
三、新建表orders:
列1:id,整型,主键
列2:name,字符串,最大长度100,不能为空
列3:price,浮点数
列4:quantity,整型
列5:category,字符串,最大长度50
create table orders(
id int primary key,
name varchar(100) not null,
price float(10,2),
quantity int,
categoty varchar(50)
);
desc orders;
四、新建表invoices:
列1:number,整型,主键自增长
列2:order_id,整型,外键关联到orders表的id列
列3:in_date:日期型
列4:total_amount:浮点型,要求数据大于0
create table invoices(
number int primary key auto_increment,
order_id int,
in_date datetime,
total_amount float(7,2),
check(total_amount>0),
foreign key(order_id)references orders(id)
);
desc invoices;