CONSTRAINTS ???

Go down

CONSTRAINTS ???

Post by praveen on Fri Mar 19, 2010 12:00 pm

Can someone tell how to use diff Constraints like primary key , unique , foreign key etc ... with some examples......

Thanx in advance....
avatar
praveen

Posts : 7
Points : 13
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile http://www.praveenbob.blogspot.com

Back to top Go down

Re: CONSTRAINTS ???

Post by Christopher on Fri Mar 19, 2010 4:29 pm

Few Examples

PRIMARY KEY CONSTRAINT

Code:
ALTER TABLE STUDENT_DETAILS ADD CONSTRAINT <constraint_name> PRIMARY KEY(ROLLNO);


FOREIGN KEY

Code:
ALTER TABLE STUDENT_MARKS_HISTORY ADD CONSTRAINT <constraint_name> FOREIGN KEY (ROLLNO) REFERENCES STUDENT_DETAILS(ROLLNO);


UNIQUE KEY

Code:
ALTER TABLE STUDENT_DETAILS ADD CONSTRAINT <constraint_name> UNIQUE(NAME);
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

while creating ??

Post by praveen on Fri Mar 19, 2010 5:59 pm

Thanx.........

these are applicable only after creating table rite???

how to make some field as a foreign key , while creating a table ?
avatar
praveen

Posts : 7
Points : 13
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile http://www.praveenbob.blogspot.com

Back to top Go down

Re: CONSTRAINTS ???

Post by parwani_ravi on Fri Mar 19, 2010 6:14 pm

@praveen

the column name should be same and use the keyword REFERENCES
eg. let there is table emp(empid,empname)

now to create foreign key in dept table v can use
dept(deptid, empid number references emp)

and the referenced column should be primary key.

parwani_ravi

Posts : 4
Points : 55
Join date : 2010-03-09
Age : 31
Location : Haldia

View user profile

Back to top Go down

Re: CONSTRAINTS ???

Post by praveen on Fri Mar 19, 2010 6:43 pm

@parwani_ravi

Thank u ........
avatar
praveen

Posts : 7
Points : 13
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile http://www.praveenbob.blogspot.com

Back to top Go down

Re: CONSTRAINTS ???

Post by Christopher on Fri Mar 19, 2010 6:54 pm

its not necessary tat foreign key has to be in the same name.

Code:

CREATE TABLE
          STUDENT_MARK_HISTORY (RNO NUMBER(4)
                          CONSTRAINT <constraint_name> REFERENCES STUDENT_DETAILS(ROLLNO),
                                      NAME VARCHAR2(20), AGE NUMBER(2))
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: CONSTRAINTS ???

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum