2017-01-23 6 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 권한을 만듭니다.
'범주'테이블의 ID 열에 대한 명시 적 값은 열 목록이 사용되고 IDENTITY_INSERT가 ON 일 때만 지정할 수 있습니다.

다른 사람이 여러 테이블을 만들고 그 중 하나에 동일한 값으로 값을 삽입하는 구체적인 방법을 설명하면 기쁠 것입니다.

+0

이 코드를 확인하십시오. CREATE DATABASE Movies; 영화 사용; – 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

그게 전부입니다. 고맙습니다! –