简体   繁体   English

我可以创建一个这样的SQL表吗?

[英]Can I Create a SQL Table Like this With to tables connectin to one?

Create Table Movie (
    ID int not null IDENTITY(1,1) Primary Key,
    Title varchar(50),
    ReleaseDate date not null,
    Genre varchar(50),
    Price decimal(18,2),
    Rating varchar(50),
    Autors_ID int FOREIGN KEY REFERENCES Movie_Autors(Autors_ID)
)

Create Table Autors (
    ID int not null IDENTITY(1,1) Primary Key,
    Name varchar(50),
    Born date not null,
    About text,
    Movie_ID int FOREIGN KEY REFERENCES Movie_Autors(Movie_ID)
)

Create Table Movie_Autors (
    ID int not null IDENTITY(1,1) Primary Key,
    Movie_ID int FOREIGN KEY REFERENCES Movie(ID),
    Autors_ID int FOREIGN KEY REFERENCES Autors(ID),
)

Presumably, this is what you want: 想必这是您想要的:

Create Table Movie (
    ID int not null IDENTITY(1,1) Primary Key,
    Title varchar(50),
    ReleaseDate date not null,
    Genre varchar(50),
    Price decimal(18,2),
    Rating varchar(50)
);

Create Table Autors (
    ID int not null IDENTITY(1,1) Primary Key,
    Name varchar(50),
    Born date not null,
    About varchar(max)
);

Create Table Movie_Autors (
    ID int not null IDENTITY(1,1) Primary Key,
    Movie_ID int FOREIGN KEY REFERENCES Movie(ID),
    Autors_ID int FOREIGN KEY REFERENCES Autors(ID),
);

That is, you have no foreign key references in Movies or Autors , so you should not declare any. 也就是说,在MoviesAutors没有外键引用,因此您不应声明任何外键引用。

As a note NOT NULL and PRIMARY KEY are redundant. 注意, NOT NULLPRIMARY KEY是多余的。 PRIMARY KEY is sufficient. PRIMARY KEY就足够了。

I also changed text to varchar(max) . 我也将text更改为varchar(max) The text datatype has been deprecated. text数据类型已被弃用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM