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

时间:2018-10-22 11:42:17

标签: c# sql sql-server

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),
)

1 个答案:

答案 0 :(得分:2)

大概是您想要的:

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),
);

也就是说,您在MoviesAutors中没有外键引用,因此您不应声明任何外键引用。

请注意,NOT NULLPRIMARY KEY是多余的。 PRIMARY KEY就足够了。

我也将text更改为varchar(max)The text datatype has been deprecated.