mysql 数据库创建 自己 心得

在找工作的我 努力实现自己的梦想

在创建自己的数据库事 发现自己连 主键 和 外键 ,   索引 都创建不了。。花了一天的时间去寻找我想要的答案ing....

原来 自己 在创建数据库的同时 忘记数据的一致性 ,

cretae table a

(id int primary ke not null,

name varchar(40) nto null

)type=innodb;

 

create table b

(bid int primary key not null,

aid varchar(40) not null,

btext  varchar(40) not null

)type=innodb;

红色部分要注意。

蓝色部分必须的。

还有就是 创建主键见 直接 写表里  bid int primary key not null

或者外部创建也行 alter table  (table_name) add constraint  pk_bid primary key (bid); 

创建索引 表内创建: index [indexName] (  aid  )

create table b

(bid int primary key not null,

aid varchar(40) not null,

btext  varchar(40) not null,

index [indexName] (  aid  )

)type=innodb;

表外创建:

alter tabel table_name add index indx_name (column_list)

样板:

alter tabel  b  add index indx_name    aid  ; 

现在创建好索引了 可以创建外键了 因为 创建外键必须副表 要创建的那个值要先创建索引 

同样表内创建:  constraint fk_name foreign key  (aid) references a(id) on update cascade on delete set null 

create table b

(bid int primary key not null,

aid varchar(40) not null,

btext  varchar(40) not null,

index [indexName] (  aid  ),

constraint fk_name foreign key  (aid) references a(id) on update cascade on delete set null

)type=innodb;

外部创建:

alter table  (table_name)  add constraint fk_name foreign key (column_name_from) references other_table_name(column_name_to) on update cascade on delete set null;

样板:

alter table  b add constraint fk_name foreign key  aid  references  a(id)  on update cascade on delete set null;

 

 

 

 

 

转载于:https://www.cnblogs.com/szwangwei/archive/2013/03/08/2950906.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值