2017-01-23 91 views
-1

我想同時在數據庫中創建多個表並向其中插入值。 我正在使用SQL Server Management Studio。 這是我的代碼:創建多個數據庫並向其中插入多個值。 SQL

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

這就是我得到的錯誤:

在數據庫中創建 '主' 否認DATABASE權限。
僅當使用列列表且IDENTITY_INSERT爲ON時,才能指定表'類別'中標識列的顯式值。

我會很高興,如果有人解釋了創建多個表的具體細節,並在同一個語句中插入所有的值。

+0

檢查這個代碼CREATE DATABASE電影; USE電影; – Serg

+0

你正在使用哪個dbms?您遇到產品特定問題。 – jarlh

+0

你有'CategoryName'和'Notes'這兩列,但是嘗試插入3個值:'VALUES('Documentary','drama','some notes')' – Filburt

回答

0

您需要使用USE命令創建後選擇數據庫。例如

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'); 
+0

就是這樣。謝謝! –