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

そして、それは私が取得エラーです:

は、データベース 'マスタ' で拒否されたCREATE 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

それです。ありがとうございました! –

関連する問題