我正在尝试使用ALTER TABLE在MYSQL中创建外键。我已经创建并检查了主键。这就是我到目前为止所做的。任何帮助将不胜感激。
DROP TABLE IF EXISTS CUSTOMER;
CREATE TABLE CUSTOMER (
CUS_CODE int,
CUS_LNAME varchar(15),
CUS_FNAME varchar(15),
CUS_INITIAL varchar(1),
CUS_AREACODE varchar(3),
CUS_PHONE varchar(8),
CUS_BALANCE float(8)
);
ALTER TABLE CUSTOMER
ADD CONSTRAINT pk_cust_code primary key(CUS_CODE);
ALTER TABLE CUSTOMER
ADD CONSTRAINT fk_cus_lname FOREIGN KEY(CUS_LNAME)REFERENCES CUSTOMER(CUS_LNAME);
INSERT INTO CUSTOMER VALUES('10010','Ramas','Alfred','A','615','844-2573','0');
INSERT INTO CUSTOMER VALUES('10011','Dunne','Leona','K','713','894-1238','0');
INSERT INTO CUSTOMER VALUES('10012','Smith','Kathy','W','615','894-2285','345.86');
INSERT INTO CUSTOMER VALUES('10013','Olowski','Paul','F','615','894-2180','536.75');
INSERT INTO CUSTOMER VALUES('10014','Orlando','Myron','','615','222-1672','0');
INSERT INTO CUSTOMER VALUES('10015','O''Brian','Amy','B','713','442-3381','0');
INSERT INTO CUSTOMER VALUES('10016','Brown','James','G','615','297-1228','221.19');
INSERT INTO CUSTOMER VALUES('10017','Williams','George','','615','290-2556','768.93');
INSERT INTO CUSTOMER VALUES('10018','Farriss','Anne','G','713','382-7185','216.55');
INSERT INTO CUSTOMER VALUES('10019','Smith','Olette','K','615','297-3809','0');
答案 0 :(得分:0)
ALTER TABLE订单 添加外键(PersonID)参考人员(PersonID);
这是mysql中的alter query格式
答案 1 :(得分:0)
这对我有用:
ALTER TABLE State ADD FOREIGN KEY (country_id) REFERENCES Country(id);