问题描述
我创建了2个包含2个主键的表。我必须在第三个表中访问2个主键作为外键。在编写代码时,我在访问主键时出错。
代码如下:
表客户端
创建表客户端(
Id_Client nvarchar(10)not null主键,
First_Name nvarchar(30),
Last_Name nvarchar(30),
地址nvarchar(50),
Phone_Number varchar(10))
表产品
创建表产品(
Id_Product nvarchar(10)主键,
Product_Name nvarchar(30),
Measuring_Unit nvarchar(10),
Unit_Price int,
数量int)
表发票
创建表发票(
Id_Invoice nvarchar(10)主键,
Id_Client nvarchar(10),
Id_Product nvarchar(10),
Total_Cost int,
Invoice_Status int
Invoice_Date datetime)
在表格发票中我必须打电话给来自客户端表的id_client和来自product表的id_product作为外键。怎么做????
I have created 2 tables with 2 primary keys. I have to access the 2 primary keys as foreign keys in the third table. On writing the code i am getting error in accessing the primary keys.
The code is mentioned below:
Table Client
create table Client(
Id_Client nvarchar(10) not null primary key,
First_Name nvarchar(30),
Last_Name nvarchar(30),
Address nvarchar(50),
Phone_Number varchar(10))
Table Product
create table Product(
Id_Product nvarchar(10) primary key,
Product_Name nvarchar(30),
Measuring_Unit nvarchar(10),
Unit_Price int,
Quantity int)
Table Invoice
create table Invoice(
Id_Invoice nvarchar(10) primary key,
Id_Client nvarchar(10),
Id_Product nvarchar(10),
Total_Cost int,
Invoice_Status int
Invoice_Date datetime)
In the table invoice i have to call the id_client from the client table and id_product from the product table as foreign key. How to do it????
推荐答案
create table client(
id_client int IDENTITY(1,1) NOT NULL PRIMARY KEY,
name nvarchar(100),
address nvarchar(100)
)
create table product(
id_product int IDENTITY(1,1) NOT NULL PRIMARY KEY,
price numeric(10,3) NOT NULL
)
create table invoice(
id_invoice int NOT NULL IDENTITY(1,1) PRIMARY KEY,
fk_client int NOT NULL FOREIGN KEY REFERENCES client(id_client),
invoice_number nvarchar(20) unique,
salesdate date
)
create table invoice_details(
id_invoice_detail int IDENTITY(1,1) NOT NULL PRIMARY KEY,
fk_invoice int FOREIGN KEY REFERENCES invoice(id_invoice),
fk_product int FOREIGN KEY REFERENCES product(id_product),
quantity numeric(10,3) NOT NULL
)
这篇关于根据第三个表上2个表的2个主键创建2个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!