Setting a Unique Constraint
|
/* mysql> Drop TABLE Employee; Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE Employee ( -> Name VARCHAR(50) NOT NULL, -> PhoneNo VARCHAR(15) DEFAULT 'Unknown Phone' NOT NULL, -> CONSTRAINT MyUniqueKey UNIQUE (Name)); Query OK, 0 rows affected (0.07 sec)
mysql> Describe Employee; +---------+-------------+------+-----+---------------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------------+-------+ | Name | varchar(50) | | PRI | | | | PhoneNo | varchar(15) | | | Unknown Phone | | +---------+-------------+------+-----+---------------+-------+ 2 rows in set (0.00 sec)
mysql> INSERT INTO Employee (Name, Phone) VALUES ('Joe Yin', '666 2323'); ERROR 1054 (42S22): Unknown column 'Phone' in 'field list' mysql> INSERT INTO Employee (Name) VALUES ('John Doe'); Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Employee (Name, Phone) VALUES ('Joe Yin', NULL); ERROR 1054 (42S22): Unknown column 'Phone' in 'field list' mysql> Select * from Employee; +----------+---------------+ | Name | PhoneNo | +----------+---------------+ | John Doe | Unknown Phone | +----------+---------------+ 1 row in set (0.00 sec)
*/ Drop TABLE Employee; CREATE TABLE Employee ( Name VARCHAR(50) NOT NULL, PhoneNo VARCHAR(15) DEFAULT 'Unknown Phone' NOT NULL, CONSTRAINT MyUniqueKey UNIQUE (Name));
Describe Employee;
INSERT INTO Employee (Name, Phone) VALUES ('Joe Yin', '666 2323'); INSERT INTO Employee (Name) VALUES ('John Doe'); INSERT INTO Employee (Name, Phone) VALUES ('Joe Yin', NULL);
Select * from Employee;
|
|
|
|
|
Related Scripts with Example Source Code in same category :
-
-
|
|