【问题标题】:Creating several databases and inserting multiple values into them. SQL [duplicate]创建多个数据库并将多个值插入其中。 SQL [重复]
【发布时间】:2017-06-07 16:52:28
【问题描述】:

我想同时在数据库中创建多个表并将值插入其中。 我正在使用 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 时,才能为表“Categories”中的标识列指定显式值。

如果有人在同一个语句中解释创建多个表并在所有表中插入值的细节,我会很高兴。

【问题讨论】:

  • 检查此代码创建数据库电影;使用电影;
  • 您使用的是哪个 dbms?您遇到了特定于产品的问题。
  • 您有 CategoryNameNotes 列,但尝试插入 3 个值:VALUES('Documentary', 'drama', 'some notes')

标签: sql sql-server database create-table


【解决方案1】:

您需要在使用 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');

【讨论】:

  • 就是这样。谢谢!
猜你喜欢
  • 2018-11-05
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
相关资源
最近更新 更多