Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Dropping tables with keys to each other

Author  Topic 

kenshinofkin
Starting Member

3 Posts

Posted - 2008-11-21 : 19:00:58
I am having a problem with dropping two tables that have keys to each other. I am trying to drop the key before dropping the table and am having not luck with the synax for dropping the key. Can anyone help?


--
-- SQL to drop all our tables
--
IF OBJECT_ID('GlucoseReading') IS NOT NULL
DROP TABLE GlucoseReading
GO

IF OBJECT_ID('DiabeticAccount') IS NOT NULL
DROP TABLE DiabeticAccount
GO

IF OBJECT_ID('DoctorAccount') IS NOT NULL
DROP TABLE DoctorAccount
GO

IF OBJECT_ID('Diabetic') IS NOT NULL
ALTER TABLE Diabetic
DROP doctorID
DROP TABLE Diabetic
GO

IF OBJECT_ID('Doctor') IS NOT NULL
ALTER TABLE Doctor
DROP diabeticID
DROP TABLE Doctor
GO

--
-- SQL to create our tables.
--
Create Table Diabetic (
diabeticID INT IDENTITY PRIMARY KEY,
name VARCHAR(100) NOT NULL,
diabetesType BIT
)

Create Table DiabeticAccount (
diabeticAccountID INT IDENTITY PRIMARY KEY,
diabeticID INT REFERENCES Diabetic NOT NULL,
userName VARCHAR(16) NOT NULL,
password VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
lastIPAddress VARCHAR(11) NOT NULL,
lastActive DATETIME NOT NULL
)

Create Table GlucoseReading (
glucoseReadingID INT IDENTITY PRIMARY KEY,
diabeticID INT REFERENCES Diabetic NOT NULL,
glucoseResult FLOAT NOT NULL,
glucoseDateTime DATETIME NOT NULL,
meal BIT,
mealEntry VARCHAR(MAX),
exercise VARCHAR(MAX),
notes VARCHAR(MAX),
doctorNotes VARCHAR(MAX)

UNIQUE (glucoseDateTime)
)

Create Table Doctor (
doctorID INT IDENTITY PRIMARY KEY,
diabeticID INT REFERENCES Diabetic,
name VARCHAR(100) NOT NULL,
)

Create Table DoctorAccount (
doctorAccountID INT IDENTITY PRIMARY KEY,
doctorID INT REFERENCES Doctor NOT NULL,
userName VARCHAR(16) NOT NULL,
password VARCHAR(20) NOT NULL,
email VARCHAR(50) NOT NULL,
lastIPAddress CHAR(11) NOT NULL,
lastActive DATETIME NOT NULL
)

--
-- SQL to add a column to tables
ALTER TABLE Diabetic
ADD doctorID INT REFERENCES Doctor;

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-22 : 00:27:04
you need to drop the index first before dropping column.
Go to Top of Page

kenshinofkin
Starting Member

3 Posts

Posted - 2008-11-22 : 01:46:37
What are the indexes. Don't you have to create an index before you can drop it. Sorry new to this stuff.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-22 : 02:03:27
ah...my bad...i meant the foreign constraints which relates the table to each other on DoctorID & diabeticID columns before dropping them.
Go to Top of Page

kenshinofkin
Starting Member

3 Posts

Posted - 2008-11-22 : 02:20:46
Sorry again but still having problems. I tried dropping the foreign key like this:

ALTER TABLE Diabetic
DROP CONSTRAINT doctorID

But I get doctorID is not a constraint??? I also left out CONSTRAINT and still no go.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-22 : 02:41:37
quote:
Originally posted by kenshinofkin

Sorry again but still having problems. I tried dropping the foreign key like this:

ALTER TABLE Diabetic
DROP CONSTRAINT doctorID

But I get doctorID is not a constraint??? I also left out CONSTRAINT and still no go.


you need to get constraint name
try this out and get constraint name

SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE WHERE TABLE_NAME='Diabetic' AND COLUMN_NAME='doctorID'

and

SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE WHERE TABLE_NAME='Doctor' AND COLUMN_NAME='diabeticID'
Go to Top of Page
   

- Advertisement -