1 说明:此文件包含了blog数据库中建立所有的表的Mysql语句. 2 3 在sql语句中注意“约束的概念": 4 1.实体完整性约束(主键--唯一且非空) primary key() 5 违约处理:No action(拒绝执行) 6 7 2.参照完整性约束(外键约束)foregin key() references tableName(filedName) [on delete|update casecade | no action] 8 违约处理:级联更新或拒绝执行 9 10 3.用户自定义完整性约束(not null,unique,check短语) 11 违约处理:拒绝执行 12 13 //添加列语法 14 【alter table blog_article add columName type constraint】 15 添加约束例子 16 【alter table blog_article add CONSTRAINT foreign key(category_Name) references blog_category(category_Name) on delete cascade on update cascade】 17 18 19 问题:如何让相册,相片,文章公用一个评论表? 20 21 create database blog; 22 23 create table blog_user 24 ( 25 user_Name char(15) not null check(user_Name !=''),26 user_Password 27 user_emial varchar(20) not unique,1)">28 primary key(user_Name) 29 30 )engine=innodb default charset=utf8 auto_increment=1; 31 32 33 34 35 create table blog_category 36 37 category_Name char(18) not null check(category_Name!=''38 category_Date datetime not 39 primary key(category_Name) 40 )engine=innod 41 42 43 44 45 create table blog_article 46 47 article_Id int unsigned not auto_increment,1)">48 article_title varchar(20) not 49 article_content longtext not 50 article_date datetime not 51 article_readTime null default 052 user_Name 53 category_Name 54 primary key(article_Id),1)">55 foreign key(user_Name) references blog_user(user_Name) on delete cascade on update cascade,1)">56 foreign key(category_Name) references blog_category(category_Name) on delete cascade on update cascade 57 )engine=innodb 58 59 60 61 62 63 64 CREATE TABLE blog_comment ( 65 comment_Id int(10) unsigned NOT NULL AUTO_INCREMENT,1)">66 comment_Content varchar(90) NOT NULL,1)">67 comment_Date datetime NOT NULL,1)">68 article_Id ) unsigned NOT NULL,1)">69 user_Name char(1570 PRIMARY KEY (comment_Id),1)">71 foreign key(article_Id) references blog_article(article_Id) on delete cascade on update cascade,1)">72 foreign key(user_Name) references blog_user(user_Name) on delete cascade on update cascade 73 )engine=innodb 74 75 76 77 create table blog_photoAlbum 78 79 photoAlbum_Name char(20) not null check(photoAlbum_Name!=''80 photoAlbum_Date datetime not 81 primary key(photoAlbum_Name) 82 )engine=innodb default charset=utf8; 83 84 85 86 87 create table blog_photograph 88 89 photograph_Name varchar(20) not null check(photograph_Name!=''90 photograph_Date datetime not 91 photoAlbum_Name char(20) not 92 photoURL varchar(90) not 93 foreign key(photoAlbum_Name) references blog_photoAlbum(photoAlbum_Name) on delete cascade on update cascade 94 )engine=innodb default charset=utf8;
原文地址:https://www.cnblogs.com/xiadongqing
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。