oracle Sql constraint


Published on

Published in: Technology
1 Like
  • @viditkothari1 thank U for U'R comment but
    can you determine which slide ??
    I can't see any slide have the same color with background ?
    Are you sure you want to  Yes  No
    Your message goes here
  • The content is very nice and easy to understand, but has got a very serious issue. There are slides with poor color scheme because of which they get matched with the background color and become illegible.

    I recommend you update this presentation with a updated consistent color scheme
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

oracle Sql constraint

  1. 1. SQLConstraintMade By: Israa Al- Jarrah CIS Dept-- IT Faculty
  2. 2. SQL Constraint Meaning Write Constraint Constraint Type
  3. 3.  Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement) Constraints are used to limit the type of data that can go into a table.
  4. 4. Constraint [Constraint name] Constraint Type
  5. 5. You can define constraints syntactically in two ways: 1. As part of the definition of an individual column or attribute. This is called inline specification. 2. As part of the table definition. This is called out-of-line specification.
  6. 6. Types of constraints: 1.Integrity constraints 2.Value constraintsTable constraint Restricts data value with respect to all other values in table
  7. 7. 1. Not Null2. Unique Key3. Primary Key4. Foreign Key5. Check Key6. Default Key
  8. 8. Not Null constraint enforces a column to NOT accept NULL values. constraint enforces a column to NOT accept NULL values. This means that you cannot insert a new record, or update a record without adding a value to this field. Expression of Not Null we use short cut NN
  9. 9. 1. Not Null Example :-Create Table Language( Language_Name varchar2(30), Language_Id number(15)ConstraintConstraint_Language_language_name_NN Not Null);
  10. 10. All other constraintscan be declared either inline or out of line.
  11. 11. 2. Unique Key The UNIQUE constraint uniquely identifies each record in a database table.
  12. 12. 2. Unique Key ExampleCreate Table Person( Person_Id number(15) ConstraintPerson_person_id_UN Unique Person_name varchar2(40), Person_BD Date);
  13. 13. 2. Unique Key ExampleCreate Table Person( Person_Id number(15), Person_name varchar2(40), Person_BD Date Constraint Person_person_id_UN Unique (Person_Id)
  14. 14. 3. Primary Key Primary keys must contain unique values  A primary key column cannot contain NULL values.  Each table should have a primary key, and each table can have only ONE primary key
  15. 15. 3. Primary Key ExampleCREATE TABLE locations_demo location_id NUMBER(4) ConstraintConstraint_loc_id_pk PRIMARY KEY , street_address VARCHAR2(40) , postal_code VARCHAR2(12));
  16. 16. 3. Primary Key ExampleCREATE TABLE locations_demo location_id NUMBER(4) , street_address VARCHAR2(40) , postal_code VARCHAR2(12) , Constraint Constraint_loc_id_pkPRIMARY KEY (Location_id));
  17. 17. Unique KeyUnique Key Primary Key
  18. 18. Unique Key Unique Key accept only one null value  unique key use many time in table.
  19. 19. Primary Key Unique KeyPrimary key does not accept null value  primary key use only one time in table.
  20. 20. 4. Foreign Key Unique Key A FOREIGN KEY in one table points to a PRIMARY KEY in another table  The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
  21. 21. 4. Foreign Key Example Unique KeyThe "Persons" table:P-Id LastName FirstName Address CIty 1 Hansen Ola Timoteivn Sandnes 10 2 Svendson Tove Borgvn23 Sandnes 3 Pettersen Kari Storagt 20 Sandnes
  22. 22. 4. Foreign Key Example Unique KeyThe “Order" table: O_Id OrderNo P_id 1 77895 3 2 44678 3 3 22456 2 4 24562 1
  23. 23. 4. Foreign Key Example "P_Id" column in the "Orders" table points to the "P_Id" column in the "Persons" table. The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table. The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
  24. 24. 4. Foreign Key Example First step must create “Person” TableCreate Table Person( P_Id number Constraintconstraint_person_person_Id_PK primary key, LastName varcahr2(45), FirstName varchar2(45), Address varchar2(45), City varchar2(45));
  25. 25. 4. Foreign Key ExampleCreate Table Order(Order_id number(15) CONSTRAINTcon_Order_id_pk Primary Key,OredrNO number(30) ,P_id number(15) CONSTRINTcon_person_order_id_FK refernace Person (p_id));
  26. 26. 5. Check Key The CHECK constraint is used to limit the value range that can be placed in a column.  If you define a CHECK constraint on a single column it allows only certain values for this column
  27. 27. 5. Check Key ExampleCREATE TABLE divisions (div_no NUMBER CONSTRAINT check_divnoCHECK(div_no BETWEEN 10 AND 99),div_name VARCHAR2(9) CONSTRAINTcheck_divname_cc CHECK(div_name = UPPER(div_name)));
  28. 28. 5. Check Key 6. Default Key The DEFAULT constraint is used to insert a default value into a column  The default value will be added to all new records if no other value is specified
  29. 29. 5. Check Key Example 6. Default KeyCREATE TABLE Persons( P_Id Number(15), LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT Sandnes);
  30. 30. CONSTRAINT constraint_namePRIMARY KEY (columnname1,columnname2 …)
  31. 31. Create table person( Person_ID Number(15), Person_phone number(15), Constraint con_person_un Unique(Person_ID,Person_Phone));
  32. 32. Reference
  33. 33. Thank you for listen Israa jarrah