从 ERD 创建 SQL table,实现所有约束

Create a SQL table from an ERD, implementing all constraints

这是 ERD 图,我将从中编写 SQL 查询:

这是针对 Oracle 的。您可以在没有 FOREIGN KEY:

的情况下类似地创建 PRODUCT table
  CREATE TABLE Orders (
        OrderID int NOT NULL,
        OrderDate date,
        ProductID int,
        PRIMARY KEY (OrderID),
        CONSTRAINT FK_PersonOrder FOREIGN KEY (ProductID)
        REFERENCES PRODUCT(ProductID)
    );

Sql 服务器

产品Table

CREATE TABLE product 
  ( 
     productid   INT PRIMARY KEY IDENTITY(1, 1), 
     productname VARCHAR(50), 
     unitprice   NUMERIC(17, 2) 
  ) 

订单Table

CREATE TABLE orders 
  ( 
     orderid     INT PRIMARY KEY IDENTITY(1, 1), 
     OrderDate Datetime, 
     productid   INT, 
     FOREIGN KEY (productid) REFERENCES product(productid) 
  ) 
CREATE TABLE product 
  ( 
     ProductId int,
     ProductnName varchar(50) not null, 
     UnitPrice   NUMERIC(10, 2) 
     CONSTRAINT PRIMARY KEY (ProductId)
  ) 


CREATE TABLE orders 
  ( 
     orderid int auto_increment, 
     OrderDate Datetime, 
     ProductId int, 
     CONSTRAINT PRIMARY KEY (orderid)
     CONSTRAINT FOREIGN KEY (ProductId) REFERENCES product(ProductId)

  )