Jump to content



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 post
Share on other sites
  • 8 months later...



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




Kolla Sanjeeva Rao

OCP Oracle.



Link to post
Share on other sites


This topic is now archived and is closed to further replies.

  • 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.