mysql  constraints

The mysql constraints are used to specify rules for the data in a table.they are placed on columns or tables.

the additional constraints or modifiers can be used to increase the consistency of the data that will be entered into it.

the constraints can be defined in create table or alter table statements.

MySQL Constraint syntax :

          create table     table_name
                             
                             (
                             
                             column_name1           data_type(size)           constraint_name,
                             
                             column_name2           data_type(size)           constraint_name,
                             
                             ........
                             
                             );

the mysql provides the following constraints:

constraints in MySQL

  • not nullindicate a column cannot have null value
  • uniquethe unique constraint ensures, that all data are unique in a column
  • primary keythe primary key constraint uniquely identifies each record in a                            database table.primary keys cannot be null.
  • foreign keythe foreign key ensure the referential integrity of the data in one                            table to match values in another table
  • defaultused to specify the default value of the column
  • enum :  an enum is a string object with a value chosen from a list of permitted values
  • setprovide a way define columns that can contain only a given set of values