User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 391,568 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,644 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 1312 | Replies: 1
Reply
Join Date: Aug 2007
Location: Bangalore, India
Posts: 101
Reputation: ChaseVoid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 10
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Applying Constraints after creating a table.

  #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?

CREATE DATABASE HomeInventory

USE HomeInventory

CREATE TABLE Item
(
ItemID int CONSTRAINT pk1 PRIMARY KEY IDENTITY NOT NULL,
LocationID int CONSTRAINT fk1 FOREIGN KEY REFERENCES dbo.Location(LocationID) NOT NULL,
Description varchar(50) NOT NULL,
Quantity int,
PurchaseDate smalldatetime,
SerialNumber varchar(50),
Cost smallmoney CONSTRAINT ck1 CHECK Cost >= 0,
ItemNotes varchar(250)
)

CREATE TABLE Location
(
LocationID int CONSTRAINT pk2 PRIMARY KEY IDENTITY NOT NULL,
Location varchar(50) NOT NULL
)

CREATE TABLE Photo
(
PhotoID int CONSTRAINT pk3 PRIMARY KEY IDENTITY NOT NULL,
ItemID int CONSTRAINT fk2 FOREIGN KEY REFERENCES dbo.Item(ItemID),
PhotoLocation varchar(100) NOT NULL,
PhotoCaption varchar(50),
PhotoNotes varchar(250)
)

CREATE INDEX Item_Serial_Number_IX 
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2006
Location: Egypt
Posts: 759
Reputation: RamyMahrous is on a distinguished road 
Rep Power: 4
Solved Threads: 57
Featured Poster
RamyMahrous's Avatar
RamyMahrous RamyMahrous is offline Offline
Master Poster

Re: Applying Constraints after creating a table.

  #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..
B.Sc Computer Science, Helwan University
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 10:14 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC