我想在数据库中同时创建几个表并将值插入其中。 我正在使用SQL Server Management Studio。 那是我的代码:
CREATE DATABASE Movies
CREATE TABLE Directors (
Id int PRIMARY KEY IDENTITY,
DirectorName nvarchar(50) NOT NULL,
Notes nvarchar(1000)
);
INSERT INTO Directors (DirectorName, Notes)
VALUES ('John', 'some notes'),
('John', 'some notes'),
('John', 'some notes'),
('John', 'some notes'),
('John', 'some notes');
CREATE TABLE Genres (
Id int PRIMARY KEY IDENTITY,
GenreName nvarchar(50) NOT NULL,
Notes nvarchar(1000)
);
INSERT INTO Genres (GenreName, Notes)
VALUES ('drama', 'some notes'),
('drama', 'some notes'),
('drama', 'some notes'),
('drama', 'some notes'),
('drama', 'some notes');
CREATE TABLE Categories (
Id int PRIMARY KEY IDENTITY,
CategoryName nvarchar(50) NOT NULL,
Notes nvarchar(1000)
);
INSERT INTO Categories (CategoryName, Notes)
VALUES ('Documentary', 'drama', 'some notes'),
('Documentary', 'drama', 'some notes'),
('Documentary', 'drama', 'some notes'),
('Documentary', 'drama', 'some notes'),
('Documentary', 'drama', 'some notes');
CREATE TABLE Movies (
Id int PRIMARY KEY IDENTITY,
Title nvarchar(50) NOT NULL,
DirectorId int NOT NULL,
CopyrightYear date,
Length int,
GenreId int,
CategoryId int,
Rating int,
Notes nvarchar(1000)
);
INSERT INTO Movies (
Title,
DirectorId,
CopyrightYear,
Length,
GenreId,
CategoryId,
Rating,
Notes )
VALUES ('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes'),
('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes'),
('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes'),
('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes'),
('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes');
这就是我得到的错误:
在数据库' master'中拒绝CREATE DATABASE权限 表格'类别'中的标识列的显式值只能在使用列列表且IDENTITY_INSERT为ON时指定。
如果有人解释了在同一语句中创建多个表并在所有表中插入值的详细信息,我会很高兴。
答案 0 :(得分:0)
您需要在使用USE命令创建数据库后选择数据库。 e.g。
CREATE DATABASE Movies
USE Movies -- You need this line to use the newly created database
CREATE TABLE Directors (
Id int PRIMARY KEY IDENTITY,
DirectorName nvarchar(50) NOT NULL,
Notes nvarchar(1000)
);
INSERT INTO Directors (DirectorName, Notes)
VALUES ('John', 'some notes'),
('John', 'some notes'),
('John', 'some notes'),
('John', 'some notes'),
('John', 'some notes');
CREATE TABLE Genres (
Id int PRIMARY KEY IDENTITY,
GenreName nvarchar(50) NOT NULL,
Notes nvarchar(1000)
);
INSERT INTO Genres (GenreName, Notes)
VALUES ('drama', 'some notes'),
('drama', 'some notes'),
('drama', 'some notes'),
('drama', 'some notes'),
('drama', 'some notes');
CREATE TABLE Categories (
Id int PRIMARY KEY IDENTITY,
CategoryName nvarchar(50) NOT NULL,
Notes nvarchar(1000)
);
INSERT INTO Categories (CategoryName, Notes)
VALUES ('Documentary', 'drama', 'some notes'),
('Documentary', 'drama', 'some notes'),
('Documentary', 'drama', 'some notes'),
('Documentary', 'drama', 'some notes'),
('Documentary', 'drama', 'some notes');
CREATE TABLE Movies (
Id int PRIMARY KEY IDENTITY,
Title nvarchar(50) NOT NULL,
DirectorId int NOT NULL,
CopyrightYear date,
Length int,
GenreId int,
CategoryId int,
Rating int,
Notes nvarchar(1000)
);
INSERT INTO Movies (
Title,
DirectorId,
CopyrightYear,
Length,
GenreId,
CategoryId,
Rating,
Notes )
VALUES ('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes'),
('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes'),
('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes'),
('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes'),
('Dumbo', 1, '1923-07-09', 180, 1, 1, 10, 'some notes');