前言
为mysql数据表建立主外键需要注意以下几点:
需要建立主外键关系的两个表的存储引擎必须是InnoDB。
外键列和参照列必须具有相似的数据类型,即可以隐式转换的数据类型。
外键列和参照列必须创建索引,如果外键列不存在索引,mysql将自动创建索引。
一、SQL语句创建数据表并设置主外键关系
create table demo.ChineseCharInfo
(
ID int not null auto_increment,
Hanzi varchar(10) not null
1、把主键定义为自动增长标识符类型MySql在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值。例如: 代码如下:create table customers(id int auto_increment primary key not null, name varchar(15));insert into customers(name) values(“name1”),(“name2”);select id from customers;以上sql语句先
本文实例讲述了mysql非主键自增长用法。分享给大家供大家参考,具体如下:
mysql并非只有主键才能自增长,而是设为键的列就可以设置自增长。 如下:
CREATE TABLE t1 (
id INT,
col1 INT auto_increment NOT NULL
);
结果如下:
如果把col1列设为键,就可以创建自增。
CREATE TABLE t1 (
id INT,
col1 INT auto_increment NOT NULL,
key(c