Add Constraint SQL - sql - sql tutorial - learn sql




 representaion of sql constraints
  • SQL Constraints are rules, which is used to limit the type of data that can go into a table.
  • To maintain the accuracy & integrity of the data within the table.
  • Constraints are used to make sure that the integrity of data is maintained in the database.
  • Constraints can be divided into two types as follows,

Column level constraints :

  • limits only column data

Table level constraints :

  • limits whole table data
  • Following are the most used constraints that can be applied to a table.
    • Not null
    • Unique
    • Primary key
    • Foreign key
    • Check
    • Default
Tags : sql tutorial , pl sql tutorial , mysql tutorial , oracle tutorial , learn sql , sql server tutorial

SQL > ALTER TABLE > Add Constraint Syntax

  • Sometimes we may decide to add a new constraint to an existing table (to see what are the different types of constraints that can be located on a database table, ref. to the CONSTRAINT section).
 adding constraints
  • The syntax for adding a constraint in SQL is,
ALTER TABLE "table_name"
ADD [CONSTRAINT_NAME]
[CONSTRAINT_TYPE][CONSTRAINT_CONDITION];
  • For example, assuming our initial point is the Employee table created in the CREATE TABLE section:

Table Employee

Column Name Data Type
First_Name char(50)
Last_Name char(50)
Address char(50)
City char(50)
Country char(25)
Birth_Date datetime
  • Assume we want to add a UNIQUE constraint to the "Address" column. To do this, we type in the following:

MySQL:

ALTER TABLE Employee ADD CONSTRAINT Wikitechy UNIQUE (Address);

Oracle:

ALTER TABLE Employee ADD CONSTRAINT Wikitechy UNIQUE (Address);

SQL Server:

ALTER TABLE Employee ADD CONSTRAINT Wikitechy UNIQUE (Address);
  • where Wikitechy is the name of the constraint.

Alter Table Add Constraint

Alter Table Add Constraint

This tutorial provides more the basic needs and informations on sql tutorial , pl sql tutorial , mysql tutorial , sql server , sqlcode , sql queries , sql , sql formatter , sql join , w3schools sql , oracle tutorial , mysql , pl sql , learn sql , sql tutorial for beginners , sql server tutorial , sql query tutorial , oracle sql tutorial , t sql tutorial , ms sql tutorial , database tutorial , sql tutorial point , oracle pl sql tutorial , oracle database tutorial , oracle tutorial for beginners , ms sql server tutorial , sql tutorial pdf

Related Searches to Add Constraint SQL