使用外键创建表

时间:2016-05-08 00:48:21

标签: mysql sql sql-server

我正在创建我有外键的表。这是陈述的一部分。出于某种原因,我无法让它发挥作用。

我做错了什么?

CREATE TABLE Doctor (

NPI  NUMBER PRIMARY KEY NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE  NUMBER  NOT NULL, 
ADDRESS VARCHAR(20) NOT NULL
CITY  VARCHAR(20) NOT NULL );



CREATE TABLE Patient (

SSN NUMBER PRIMARY KEY NOT NULL, 
INSURANCE_POLICY_ID NUMBER NOT NULL, 
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL, 
DOB DATE NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID) REFERENCES INSURANCE (INSURANCE_POLICY_ID));

1 个答案:

答案 0 :(得分:0)

您已使用SQL ServerMySql标记了您的问题,在这两个RDBMS中都没有名为Number的数据类型,但有一种数据类型可以存储它被称为的数字INTINTEGER

因此,您的表定义应如下所示:

CREATE TABLE Doctor (
NPI     INT         NOT NULL    PRIMARY KEY ,
LNAME   VARCHAR(20) NOT NULL,
FNAME   VARCHAR(20) NOT NULL,
PHONE   INT         NOT NULL,  --<-- should be a varchar since most phone numbers have a leading zero
ADDRESS VARCHAR(20) NOT NULL,
CITY    VARCHAR(20) NOT NULL );



CREATE TABLE Patient (
SSN                 INT         NOT NULL PRIMARY KEY, 
INSURANCE_POLICY_ID INT         NOT NULL, 
LNAME               VARCHAR(20) NOT NULL,
FNAME               VARCHAR(20) NOT NULL, 
DOB                 DATE        NOT NULL,
PHONE               INT         NOT NULL,
ADDRESS             VARCHAR(20) NOT NULL,
CITY                VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID) 
 REFERENCES INSURANCE (INSURANCE_POLICY_ID));