Jump to content

SQL Command CONSTRAINT Help


$Three3

Recommended Posts

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:

 

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:

 

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));

Link to comment
Share on other sites

  • 8 months later...

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.