数据库例题(check约束)

28 阅读1分钟

image.png

商品goods表:

create table goods(
	goods_id int primary key,
	goods_name varchar(64) not null default '',
	unitprice decimal(10,2) not null default 0,
		check(unitprice>=1.0 and unitprice<=9999.99),//约束条件
    	category int not null default 0,
	provider varchar(64) not null default '');

客户customer表:

create table customer(
	customer_id char(8) primary key,
	name varchar(64) not null default '',
	address varchar(64) not null default '',
	email varchar(64) unique not null,
	sex enum('男','女') not null,
	card_id char(18) 
);

购买purchase表:

create table purchase(
	order_id int unsigned primary key,
	customer_id char(8) not null default '',//外键约束在后
	goods_id int not null default 0,//外键约束在后
	nums int not null default 0,
foreign key(customer_id) references customer(customer_id),
foreign key(goods_id) references goods(goods_id)
);