创建多个数据库并向其中插入多个值。 SQL
Creating several databases and inserting multiple values into them. SQL
我想同时在数据库中创建多个表并向其中插入值。
我正在使用 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');
这就是我得到的错误:
CREATE DATABASE permission denied in database 'master'.
An explicit value for the identity column in table 'Categories' can only be specified when a column list is used and IDENTITY_INSERT is ON.
如果有人能在同一条语句中解释创建多个表并在所有表中插入值的细节,我会很高兴。
使用 USE 命令创建数据库后,您需要 select 数据库。例如
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');
我想同时在数据库中创建多个表并向其中插入值。 我正在使用 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');
这就是我得到的错误:
CREATE DATABASE permission denied in database 'master'.
An explicit value for the identity column in table 'Categories' can only be specified when a column list is used and IDENTITY_INSERT is ON.
如果有人能在同一条语句中解释创建多个表并在所有表中插入值的细节,我会很高兴。
使用 USE 命令创建数据库后,您需要 select 数据库。例如
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');