mysql新建表代码

在这里插入图片描述

完整的创建代码

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;
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值