完整的创建代码
create table orders(
orderId int primary key auto_increment not null comment '订单编号',
userId varchar(20) not null comment '所属用户编号',
businessId int not null comment '所属商家编号',
orderDate varchar(20) not null comment '订购日期',
orderTotal decimal(7,2) default 0.00 not null comment '订单总价',
daId int not null comment '所属送货地址',
orderState int not null default 0 comment '订单状态(0:未支付,1:已支付)',
foreign key(userId) references user(userId),
foreign key(businessId) references business(businessId),
foreign key(daId) references deliveyaddress(daId)
)charset=utf8;
备注用comment ,内容用单引号引起来
建完表后想要修改表中内容
--alter table 表名 add/modify/drop 操作…
1.建完表后想要给businessId添加外键
让business表的businessId作为 orders表的businessId的外键
alter table orders add foreign key(businessId) references business(businessId);
2.添加外键约束(关联字段要用括号括起来)(借鉴)
– ALTER TABLE 从表
– ADD CONSTRAINT 约束名 FOREIGN KEY (关联字段) references 主表(关联字段);
–例如:
ALTER TABLE tb_UserAndRole
ADD CONSTRAINT FK__tb_UandR_Role FOREIGN KEY (RoleID) references tb_Role(ID);
3.添加一个字段(add)
alter table ordermm add daId int not null comment '所属送货地址' ;
4.修改一个字段(modify)
alter table food modify userSex int default 1 not null comment '用户性别(1:男;0:女)';
5.删除表中字段
alter table ordermm drop orderDate;