我正在尝试在SQL Server中创建一个数据库,但它一直在抛出错误。你能看一下这个剧本,看看我是不是搞砸了?
drop database if exists cptc;
create database cptc;
use cptc;
drop table if exists staff;
create table staff (
staffID int not null auto_increment,
fname varchar(25),
lname varchar(25),
title varchar(50),
phone varchar(10),
building varchar(10),
room varchar(10),
PRIMARY KEY(staffID),
FULLTEXT (fname,lname)
)ENGINE = MYISAM;
INSERT INTO staff VALUES ('','michael','herrera','doctor','2539703420','B14','122');
INSERT INTO staff VALUES ('','holly','herrera','teacher','2534667896','B35','116');
INSERT INTO staff VALUES ('','jesse','kirsch','professor','2534567890','B12','112');
INSERT INTO staff VALUES ('','mark','wahlberg','professor','5552345678','B01','112');
INSERT INTO staff VALUES ('','philip','spears','technician','2065672345','B12','123');
INSERT INTO staff VALUES ('','andrew','jackson','teacher','2061234567','B32','101');
INSERT INTO staff VALUES ('','annie','smith','mechanic','2533345609','B23','102');
INSERT INTO staff VALUES ('','alfred','hills','teacher','2535821513','B14','103');
INSERT INTO staff VALUES ('','bobby','jones','nurse','5559876056','B10','104');
INSERT INTO staff VALUES ('','tiffany','jones','janitor','2539981265','B02','108');
更新:我认为我有错误,因为它是MySQL而不是SQL。谢谢大家为您(真的很快)的答案!
答案 0 :(得分:3)
这部分是MYSQL而不是SQL Server ENGINE = MYISAM;
这是表格在SQL Server中的样子(没有全文索引,首先需要启用全文搜索才能创建全文索引))
create table staff (
staffID int not null identity,
fname varchar(25),
lname varchar(25),
title varchar(50),
phone varchar(10),
building varchar(10),
room varchar(10),
PRIMARY KEY(staffID)
)
drop和create数据库将如下所示
USE [master]
GO
IF EXISTS (SELECT name FROM sys.databases
WHERE name = N'cptc')
DROP DATABASE cptc
GO
CREATE DATABASE [cptc]
drop table将如下所示
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'staff') AND type in (N'U'))
DROP TABLE staff
GO
最后,您需要在插入时省略标识列,例如
INSERT INTO staff VALUES ('michael','herrera','doctor','2539703420','B14','122');
如果您不省略该列,则会出现以下错误
Msg 8101,Level 16,State 1,Line 1
只有在使用列列表且IDENTITY_INSERT为ON时,才能指定表'staff'中标识列的显式值。
答案 1 :(得分:0)
答案 2 :(得分:0)
问题不明确。您正在创建一个表而不是数据库。 如果您提供实际的错误详细信息会很有帮助。
顺便说一句,你不应该为自动增量列添加任何值,这将自动由服务器分配。我想这导致了这个问题。