在引用日期的MySQL表中创建唯一键?

要在引用日期的MySQL表中创建唯一键,可以使用以下语法-

CREATE TABLE yourTableName
(
   yourIdColumnName dataType,
   yourDateColumnName dataType,
   .
   .
   .
   N
   UNIQUE KEY(yourIdColumnName, yourDateColumnName )
);

为了理解上述语法,让我们创建一个表。创建表的查询如下-

mysql> create table keyReferringToDateDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Client_Id int,
   -> ClientProjectDeadline date,
   -> ClientName varchar(100),
   -> ClientAge int,
   -> ClientCountryName varchar(20) ,
   -> UNIQUE KEY(Client_Id,ClientProjectDeadline)
   -> );

让我们使用SHOW CREATE命令检查表的所有详细信息。查询如下-

mysql> show create table keyReferringToDateDemo;

这是输出-

+------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------+
| Table                  | Create Table                        |
+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| keyReferringToDateDemo | CREATE TABLE `keyreferringtodatedemo` (`Id` int(11) NOT NULL AUTO_INCREMENT,`Client_Id` int(11) DEFAULT NULL,`ClientProjectDeadline` date DEFAULT NULL,`ClientName` varchar(100) DEFAULT NULL,`ClientAge` int(11) DEFAULT NULL,`ClientCountryName` varchar(20) DEFAULT NULL,PRIMARY KEY (`Id`),UNIQUE KEY `Client_Id` (`Client_Id`,`ClientProjectDeadline`)
) ENGINE =InnoDB DEFAULT CHARSET =utf8 |
+------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)