Share on Google+Share on Google+

Mysql Alter Constraint

Advertisement
A constraint is a property in SQL assigned to a column or the set of columns in a table that prevents certain types of inconsistent data values from being placed in the column of the table.

Mysql Alter Constraint

     

A constraint is a property in SQL assigned to a column or the set of columns in a table that prevents certain types of inconsistent data values from being placed in the column of the table. Constraints in SQL  are used to enforce the data integrity. This ensures the accuracy and reliability of the data in the database.

Understand with Example

The Tutorial illustrate an example from 'Mysql Alter Constraint'. To understand the example we simply create a table 'userform' with required fieldnames and datatypes respectively

Create table "userform"  :

+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| username | varchar(100) | YES | | | |
| fname | varchar(100) | YES | | | |
| email | varchar(100) | YES | | | |
| count | bigint(20) | YES | | 100 | |

 Alter column ID using the below Query:

The Alter Table is used to modify the table 'userform' and add ID as a Primary Key constraint.

ALTER TABLE userform ADD ID INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (ID);

After alter the table "userform"

+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | | auto_increment |
| username | varchar(100) | YES | | | |
| fname | varchar(100) | YES | | | |
| email | varchar(100) | YES | | | |
| count | bigint(20) | YES | | 100 | |

Advertisements

Advertisement

Posted on: January 16, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Comments:0