944,074 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 4153
  • MS SQL RSS
Dec 1st, 2007
0

Applying Constraints after creating a table.

Expand Post »
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?

MS SQL Syntax (Toggle Plain Text)
  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
Reputation Points: 40
Solved Threads: 12
Junior Poster
ChaseVoid is offline Offline
116 posts
since Aug 2007
Dec 5th, 2007
0

Re: Applying Constraints after creating a table.

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..
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Slow SQL performance over internet
Next Thread in MS SQL Forum Timeline: transfering data including ID's





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC