SQL Error: There is already an object named 'TPatients' in the database
SQL Error: There is already an object named 'TPatients' in the database
我似乎无法弄清楚为什么会出现以下错误:
Msg 2714, Level 16, State 5, Line 56
There is already an object named 'TPatients' in the database.
Msg 1750, Level 16, State 1, Line 56
Could not create constraint or index. See previous errors.
下面是我的 Drop Table 和 Create Table SQL 语句。每当我执行时,它都会说 TPatients 已经存在,但是我在对象资源管理器中看不到它。我试着绕过 drop table 语句,但无法让它工作。
-- --------------------------------------------------------------------------------
-- Step #1 : Drop tables
-- --------------------------------------------------------------------------------
IF OBJECT_ID ('TAppointments') IS NOT NULL DROP TABLE TAppointments
IF OBJECT_ID ('TPatients') IS NOT NULL DROP TABLE TPatients
IF OBJECT_ID ('TDoctors') IS NOT NULL DROP TABLE TDoctors
IF OBJECT_ID ('TNurses') IS NOT NULL DROP TABLE TNurses
-- --------------------------------------------------------------------------------
-- Step #1 : Create table
-- --------------------------------------------------------------------------------
CREATE TABLE TDoctors
(
intDoctorID INTEGER NOT NULL
,strFirstName VARCHAR(25) NOT NULL
,strLastName VARCHAR(25) NOT NULL
,strEmail VARCHAR(25) NOT NULL
,strPhone VARCHAR(25) NOT NULL
,strAddress VARCHAR(25) NOT NULL
,dtmHireDate DATETIME NOT NULL
,dtmTerminationDate DATETIME NULL
,dtmPassedBoardDate DATETIME NOT NULL
CONSTRAINT TDoctors_PK PRIMARY KEY ( intDoctorID )
)
CREATE TABLE TNurses
(
intNurseID INTEGER NOT NULL
,strFirstName VARCHAR(25) NOT NULL
,strLastName VARCHAR(25) NOT NULL
,strEmail VARCHAR(25) NOT NULL
,strPhone VARCHAR(25) NOT NULL
,strAddress VARCHAR(25) NOT NULL
,dtmHireDate DATETIME NOT NULL
,dtmTerminationDate DATETIME NULL
,dtmPassedBoardDate DATETIME NOT NULL
CONSTRAINT TNurses_PK PRIMARY KEY ( intNurseID )
)
CREATE TABLE TPatients
(
intPatientID INTEGER NOT NULL
,strFirstName VARCHAR(25) NOT NULL
,strLastName VARCHAR(25) NOT NULL
,dtmDateOfBirth DATETIME NOT NULL
,strEmail VARCHAR(25) NOT NULL
,strPhone VARCHAR(25) NOT NULL
,strAddress VARCHAR(25) NOT NULL
,strZipCode VARCHAR(25) NOT NULL
,strSSN VARCHAR(25) NOT NULL
,strInsuranceCompany VARCHAR(25) NOT NULL
,strEmergFirstName VARCHAR(25) NOT NULL
,strEmergLastName VARCHAR(25) NOT NULL
,strEmergPhone VARCHAR(25) NOT NULL
CONSTRAINT TPatients PRIMARY KEY ( intPatientID )
)
CREATE TABLE TAppointments
(
intAppointmentID INTEGER NOT NULL
,dtmAppointmentDate DATETIME NOT NULL
,strReason VARCHAR(50) NOT NULL
,strDiagnosis VARCHAR(25) NOT NULL
,dtmFollowUpDate DATETIME NOT NULL
,intPatientID INTEGER NOT NULL
,intNurseID INTEGER NOT NULL
,intDoctorID INTEGER NOT NULL
CONSTRAINT TAppointments PRIMARY KEY ( intAppointmentID )
)
问题是因为您为约束指定了 与 table 相同的名称。
给它们一个唯一的名称,例如,如果它是主键,则以“PK_Tpatients”作为前缀。
请注意,tables 和约束都是“对象”,因此需要唯一的名称。
我似乎无法弄清楚为什么会出现以下错误:
Msg 2714, Level 16, State 5, Line 56
There is already an object named 'TPatients' in the database.
Msg 1750, Level 16, State 1, Line 56
Could not create constraint or index. See previous errors.
下面是我的 Drop Table 和 Create Table SQL 语句。每当我执行时,它都会说 TPatients 已经存在,但是我在对象资源管理器中看不到它。我试着绕过 drop table 语句,但无法让它工作。
-- --------------------------------------------------------------------------------
-- Step #1 : Drop tables
-- --------------------------------------------------------------------------------
IF OBJECT_ID ('TAppointments') IS NOT NULL DROP TABLE TAppointments
IF OBJECT_ID ('TPatients') IS NOT NULL DROP TABLE TPatients
IF OBJECT_ID ('TDoctors') IS NOT NULL DROP TABLE TDoctors
IF OBJECT_ID ('TNurses') IS NOT NULL DROP TABLE TNurses
-- --------------------------------------------------------------------------------
-- Step #1 : Create table
-- --------------------------------------------------------------------------------
CREATE TABLE TDoctors
(
intDoctorID INTEGER NOT NULL
,strFirstName VARCHAR(25) NOT NULL
,strLastName VARCHAR(25) NOT NULL
,strEmail VARCHAR(25) NOT NULL
,strPhone VARCHAR(25) NOT NULL
,strAddress VARCHAR(25) NOT NULL
,dtmHireDate DATETIME NOT NULL
,dtmTerminationDate DATETIME NULL
,dtmPassedBoardDate DATETIME NOT NULL
CONSTRAINT TDoctors_PK PRIMARY KEY ( intDoctorID )
)
CREATE TABLE TNurses
(
intNurseID INTEGER NOT NULL
,strFirstName VARCHAR(25) NOT NULL
,strLastName VARCHAR(25) NOT NULL
,strEmail VARCHAR(25) NOT NULL
,strPhone VARCHAR(25) NOT NULL
,strAddress VARCHAR(25) NOT NULL
,dtmHireDate DATETIME NOT NULL
,dtmTerminationDate DATETIME NULL
,dtmPassedBoardDate DATETIME NOT NULL
CONSTRAINT TNurses_PK PRIMARY KEY ( intNurseID )
)
CREATE TABLE TPatients
(
intPatientID INTEGER NOT NULL
,strFirstName VARCHAR(25) NOT NULL
,strLastName VARCHAR(25) NOT NULL
,dtmDateOfBirth DATETIME NOT NULL
,strEmail VARCHAR(25) NOT NULL
,strPhone VARCHAR(25) NOT NULL
,strAddress VARCHAR(25) NOT NULL
,strZipCode VARCHAR(25) NOT NULL
,strSSN VARCHAR(25) NOT NULL
,strInsuranceCompany VARCHAR(25) NOT NULL
,strEmergFirstName VARCHAR(25) NOT NULL
,strEmergLastName VARCHAR(25) NOT NULL
,strEmergPhone VARCHAR(25) NOT NULL
CONSTRAINT TPatients PRIMARY KEY ( intPatientID )
)
CREATE TABLE TAppointments
(
intAppointmentID INTEGER NOT NULL
,dtmAppointmentDate DATETIME NOT NULL
,strReason VARCHAR(50) NOT NULL
,strDiagnosis VARCHAR(25) NOT NULL
,dtmFollowUpDate DATETIME NOT NULL
,intPatientID INTEGER NOT NULL
,intNurseID INTEGER NOT NULL
,intDoctorID INTEGER NOT NULL
CONSTRAINT TAppointments PRIMARY KEY ( intAppointmentID )
)
问题是因为您为约束指定了 与 table 相同的名称。
给它们一个唯一的名称,例如,如果它是主键,则以“PK_Tpatients”作为前缀。
请注意,tables 和约束都是“对象”,因此需要唯一的名称。