Applying Constraints after creating a table.

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Applying Constraints after creating a table.

 
0
  #1
Dec 1st, 2007
I would like to know how to apply Constraints after creating a table. I was just practicing and creating a database HomeInventory by reading the free e-book from Microsoft. But I'm trying to implement the functions using T-SQL. So How can I do so?

  1. CREATE DATABASE HomeInventory
  2.  
  3. USE HomeInventory
  4.  
  5. CREATE TABLE Item
  6. (
  7. ItemID int CONSTRAINT pk1 PRIMARY KEY IDENTITY NOT NULL,
  8. LocationID int CONSTRAINT fk1 FOREIGN KEY REFERENCES dbo.Location(LocationID) NOT NULL,
  9. Description varchar(50) NOT NULL,
  10. Quantity int,
  11. PurchaseDate smalldatetime,
  12. SerialNumber varchar(50),
  13. Cost smallmoney CONSTRAINT ck1 CHECK Cost >= 0,
  14. ItemNotes varchar(250)
  15. )
  16.  
  17. CREATE TABLE Location
  18. (
  19. LocationID int CONSTRAINT pk2 PRIMARY KEY IDENTITY NOT NULL,
  20. Location varchar(50) NOT NULL
  21. )
  22.  
  23. CREATE TABLE Photo
  24. (
  25. PhotoID int CONSTRAINT pk3 PRIMARY KEY IDENTITY NOT NULL,
  26. ItemID int CONSTRAINT fk2 FOREIGN KEY REFERENCES dbo.Item(ItemID),
  27. PhotoLocation varchar(100) NOT NULL,
  28. PhotoCaption varchar(50),
  29. PhotoNotes varchar(250)
  30. )
  31.  
  32. CREATE INDEX Item_Serial_Number_IX
  33. ON dbo.Item(SerialNumber ASC) INCLUDE (Description)

In my first table, I'm referencing a foreign key, but according I want to first create the table Item and then Location. So how do I apply the constraint later?

When I create this table I get dbo.Item, can I change or remove that dbo?

I can;t even access the table structure later by using sp_help dbo.Item. It generates an error saying syntax wrong at '.'

I'm using SQL Server 2005 Express Management Studio
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Applying Constraints after creating a table.

 
0
  #2
Dec 5th, 2007
I didn't understand you, but you can do anything(anything logic) after creating table using "alter" and in DB design we create the one table and after that the many tables..
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the MS SQL Forum


Views: 2925 | Replies: 1
Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC