《MYSQL數據庫mysql創建數據表實例代碼》要點:
本文介紹了MYSQL數據庫mysql創建數據表實例代碼,希望對您有用。如果有疑問,可以聯系我們。
在mysql數據庫中創建表:
?MYSQL應用
mysql創建表
說明:此文件包含了blog數據庫中建立所有的表的mysql語句.
?
在sql語句中注意“約束的概念":
1,實體完整性約束(主鍵--唯一且非空) primary key()
??? 違約處理:no action(拒絕執行)
?
2,參照完整性約束(外鍵約束)foregin key() references tablename(filedname) [on delete|update casecade | no action]
? 違約處理:級聯更新或拒絕執行
?
3,用戶自定義完整性約束(not null,unique,check短語)
????? 違約處理:拒絕執行
?
//添加列語法
//【alter table blog_article add columname type constraint】
//添加約束例子
//【alter table blog_article add constraint foreign key(category_name) references blog_category(category_name) on delete cascade on update cascade】
?
問題:如何讓相冊,相片,文章公用一個評論表?
?MYSQL應用
create database blog;
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;MYSQL應用
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;
(腳本學堂 www.jbxue.com)
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;MYSQL應用
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;MYSQL應用
以上分享了mysql數據庫創建表的多個實例,希望對大家有所贊助.MYSQL應用
《MYSQL數據庫mysql創建數據表實例代碼》是否對您有啟發,歡迎查看更多與《MYSQL數據庫mysql創建數據表實例代碼》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7435.html