mysql数据库创建数据表语法

在创建表时注意表中的列的约束
 
1.实体完整性约束(主键--唯一且非空) primary key()
  违约处理:No action(拒绝执行)
 
2.参照完整性约束(外键约束)foreign key() references tableName(fieldName) 
  [on delete|update cascade | no action]
  违约处理:级联更新或拒绝执行
 
3.用户自定义完整性约束(not null,unique,check短语)
  违约处理:拒绝执行

4.在对表的列添加约束最好在创建表语句的结尾处对对应的列添加相应的约束,不要在列的后面直接添加
下面这6个表创建语句对应一个完整的项目
create table blog_user
(
  user_Name char(15) not null check(user_Name !=''),
  user_Password char(15) not null,
  user_emial varchar(20) not null unique,
  primary key(user_Name)          
 
)engine=innodb default charset=utf8 auto_increment=1;

create table blog_category
(
 category_Name char(18) not null check(category_Name!=''),
 category_Date datetime not null,
 primary key(category_Name)
)engine=innod default charset=utf8 auto_increment=1;

create table blog_article
(
  article_Id int unsigned not null  auto_increment,
  article_title varchar(20) not null unique,
  article_content longtext not null,
  article_date datetime not null,
  article_readTime int unsigned not null default 0,
  user_Name char(15) not null,
  category_Name char(18) not null,
  primary key(article_Id),
  foreign key(user_Name) references blog_user(user_Name) on delete cascade on update cascade,
  foreign key(category_Name) references blog_category(category_Name) on delete cascade on update cascade
)engine=innodb default charset=utf8 auto_increment=1;

create table blog_comment (
  comment_Id int(10) unsigned NOT NULL AUTO_INCREMENT,
  comment_Content varchar(90) NOT NULL,
  comment_Date datetime NOT NULL,
  article_Id int(10) unsigned NOT NULL,
  user_Name char(15) NOT NULL,
  PRIMARY KEY (comment_Id),
  foreign key(article_Id) references blog_article(article_Id) on delete cascade on update cascade,
  foreign key(user_Name) references blog_user(user_Name) on delete cascade on update cascade
)engine=innodb default charset=utf8 auto_increment=1;

create table blog_photoAlbum
(
  photoAlbum_Name char(20) not null check(photoAlbum_Name!=''),
  photoAlbum_Date datetime not null,
  primary key(photoAlbum_Name)
)engine=innodb default charset=utf8;

create table blog_photograph
(
  photograph_Name varchar(20) not null check(photograph_Name!=''),
  photograph_Date datetime not null,
  photoAlbum_Name char(20)  not null,
  photoURL varchar(90) not null,
  foreign key(photoAlbum_Name) references blog_photoAlbum(photoAlbum_Name) on delete cascade on update cascade
)engine=innodb default charset=utf8;
在完成表的创建之后对表进行修改:
添加列语法
alter table blog_article add columName type constraint;

添加约束例子
alter table blog_articleadd constraint foreign key(category_Name)
references blog_category(category_Name)
[on delete cascade | on update cascade];
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值