SQLConstraintMade By: Israa Al- Jarrah CIS Dept-- IT Faculty
SQL Constraint Meaning Write Constraint Constraint Type
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.
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.
Types of constraints: 1.Integrity constraints 2.Value constraintsTable constraint Restricts data value with respect to all other values in table
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
1. Not Null Example :-Create Table Language( Language_Name varchar2(30), Language_Id number(15)ConstraintConstraint_Language_language_name_NN Not Null);
All other constraintscan be declared either inline or out of line.
2. Unique Key The UNIQUE constraint uniquely identifies each record in a database table.
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.
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));
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
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)));
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