比如我有一个table1,一个table2,怎样在table1中把table2的primary key 加成一列foreign key?表达式是什么?
解决方案 »
- ucs2格式怎么插入数据库
- 正则可以只返回匹配的正则数字吗?
- 请问在mysql中的映射是怎么回事啊
- Out of memory (Needed 65312 bytes)
- mysql导入sql文件命令0<**.sql
- 为什么我的mysql表记录数一超过400万就会有奇怪的问题
- 在mysql中有case when else end 像这样的用法吗??(在线等)
- 求助mysql java.sql.SQLException: No suitable driver
- 在mysql里有没有类似windows中刷新或foxbase中的ctrl+w的命令
- [MySQL][ODBC 5.2(a) Driver][mysqld-8.0.11]Query execution was interrupted
- Mysql查询结果的问题
- 请教一个mysql 命令行奇怪的现象
create table table2(a2 int primary key,
......
) engine = innodbcreate table table1(
a1 int primary key,
a2 int,
......
foreign key (a2) references table2(a2)
) engine = innodb
或用工具很容易就加上或取消外键约束了。
下面是个示例,两张表,看了就知道怎么用Sql语句写了
表一: 用户表
CREATE TABLE users (
uid int(11) NOT NULL auto_increment,
uname varchar(20) NOT NULL,
usex char(2) NOT NULL,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;表二:订单表
TABLE orders (
oid int(11) NOT NULL auto_increment,
odate timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
cid int(11) default NULL,
PRIMARY KEY (oid),
KEY cid (cid),
CONSTRAINT orders_fk FOREIGN KEY (cid) REFERENCES customer (cid)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;