mysql> create table exchange(
-> id int not null auto_increment,
-> abbrev varchar(32) not null,
-> name varchar(255) not null,
-> primary key(id));#注意: key和(id)之间没有空格
Query OK, 0 rows affected (0.01 sec)
mysql> drop table if exists exchange;
Query OK, 0 rows affected (0.00 sec)
mysql> create table exchange(
-> id int not null auto_increment,
-> abbrev varchar(32) not null,
-> name varchar(255) not null,
-> city varchar(255) null,
-> country varchar(255) null,
-> currency varchar(64) null,
-> timezone_offset datetime null,
-> created_date datetime not null,
-> last_updated_date datetime not null,
-> primary key(id))
-> engine=InnoDB auto_increment=1 default charset=utf8;
Query OK, 0 rows affected (0.02 sec)
mysql> create table data_vendor(
-> id int not null auto_increment,
-> name varchar(64) not null,
-> website_url varchar(255) null,
-> support_email varchar(255) null,
-> created_date datetime not null,
-> last_updated_date datetime not null,
-> primary key(id))
-> engine=InnoDB auto_increment=1 default charset=utf8;
Query OK, 0 rows affected (0.02 sec)
mysql> create table symbol(
-> id int not null auto_increment,
-> exchange_id int null,
-> ticker varchar(32) not null,
-> instrument varchar(64) not null,
-> name varchar(255) null,
-> sector varchar(255) null,
-> currency varchar(32) null,
-> created_date datetime not null,
-> last_updated_date datetime not null,
-> primary key(id),
-> key index_exchange_id(exchange_id))
-> engine=InnoDB auto_increment=1 default charset=utf8;
Query OK, 0 rows affected (0.02 sec)
#注意:index_exchange_id和(exchange_id))之间没有空格。Key是索引约束,对表中字段进行约束索引的,都是通过primary foreign unique等创建的。常见有foreign key,外键关联用的。 表示本表的exchange_id字段与 index_exchange_id表exchange_id字段建立外键。括号外是建立外键的对应表,括号内是对应字段 ,类似还有 KEY user(userid)
当然,key未必都是外键