在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】
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;
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;
mysql建表語句
mysql裝好以後,進入命令列,開始建表需要先建立乙個database.開始使用 create database mybase use mybase create table user id int 10 auto increment not null primary key,username va...
mysql建表語句
工作的時候總會寫一些建表語句提交給db,有的時候就會忘記主鍵自增寫法,以及一些型別的標註,下面是乙個比較全的建表語句,包括各種型別。create table minisite lock site id int not null auto increment primary key,admin id ...
mysql建表語句
create table who csjcm transfer jollycall data rec id int 11 unsigned not null auto increment comment 唯一自增主鍵 user id int 11 not null default 0 comment...