Author Topic: SQL Command CONSTRAINT Help  (Read 1594 times)

0 Members and 1 Guest are viewing this topic.

Offline $Three3Topic starter

  • Enthusiast
  • Posts: 76
  • Gender: Male
    • View Profile
SQL Command CONSTRAINT Help
« on: January 15, 2010, 01:24:48 PM »
Hi everyone, I am new to SQL and I have been reading a book called "Learning SQL" and I am stuck on this one part of the book. It has to do with the CONSTRAINT command and I am not sure what this command does or means. The book never even explains its functionality. Here is the code from the book:

Code: [Select]
CREATE TABLE person (person_id SMALLINT UNSIGNED,fname VARCHAR(20), lname VARCHAR(20), gender ENUM('M','F'), birth_date DATE, street VARCHAR(30), city VARCHAR(20), state VARCHAR(20), country VARCHAR(20), postal_code VARCHAR(20), CONSTRAINT pk_person PRIMARY KEY (person_id));
And the second table that goes along with the code above is:

Code: [Select]
CREATE TABLE favorite_food (person_id SMALLINT UNSIGNED,food VARCHAR(20), CONSTRAINT pk_favorite_food PRIMARY KEY (person_id, food), CONSTRAINT fk_fav_food_person_id FOREIGN KEY (person_id)REFERENCES person (person_id));

Offline thorpe

  • Administrator
  • 'Mind Boggling!'
  • *
  • Posts: 29,255
    • View Profile
Re: SQL Command CONSTRAINT Help
« Reply #1 on: January 15, 2010, 04:57:37 PM »
It constrains the field in question into being of a certain data.

eg; A primary key constraint means that each field must contain a unique number.

Offline ksrao

  • Irregular
  • Posts: 1
    • View Profile
Re: SQL Command CONSTRAINT Help
« Reply #2 on: September 20, 2010, 08:43:33 PM »
Hi

See what is constraint ? Constraint is basically used to impose some limitation in Data base to get the uniformity in Data Base.

Commonly used constraints
1. Primary key  - Which uniquely identifies row in a table.
2. Foreign Key  - Which is used to refers to the other table.
3. Check           - To have a validation checks on input/output
4. Not Null        - It will not allow null values.
5. Unique.       - Repetition of the values are not allowed.

To clear your basics in RDBMS go through some sql server interview questions at http://www.wiziq.com/online-class/292863-sql-server-interview-questions

Thanks
Regards
Kolla Sanjeeva Rao
OCP Oracle.