How to create a table with a unique id in MySQL
I am working on PHP and MySQL database. I have two tables in SQL Server 2005 and I want to move them to MySQL.
These two tables contain fields with Unique ID , and MySQL does not have a unique ID data type. Therefore, I cannot convert it to MySQL.
Please help me to solve this problem.
source to share
I think you are looking at how to create a primary key? There is also an auto increment that you probably need
Here's an example of creating a table:
CREATE TABLE `affiliation` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`affiliate_id` bigint(20) DEFAULT NULL,
`email_invited` varchar(255) DEFAULT NULL,
`email_provider` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8
source to share
I'm not familiar with the specifics of unique identifiers in MS SQL, but you can get what you want with the MySQL function UUID
:
https://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_uuid
I would put this in a column VARCHAR
or CHAR
with a key UNIQUE
.
source to share
Mysql has a "Unique" property.
For more information: http://dev.mysql.com/doc/refman/5.0/en/constraint-primary-key.html
If you are using software like MySQLWorkbench, you can check the attribute as primary key and unique.
source to share
MySQL knows the primary key. Read the manual. http://dev.mysql.com/doc/refman/4.1/en/constraint-primary-key.html
source to share
I think you are looking for Constraint . UNIQUE
The following SQL - query creates UNIQUE
constraints on field1
columns for a T
table:
MySQL:
CREATE TABLE 'T' (
field1 int NOT NULL,
field2 int,
UNIQUE (field)
);
To create a UNIQUE
constraint on a field1
column if the table has already been created, you can use:
ALTER TABLE T
ADD UNIQUE (field1);
To name a constraint UNIQUE
and define a constraint UNIQUE
on multiple columns, you can use:
ALTER TABLE T
ADD CONSTRAINT UC_field1_field2 UNIQUE (field1, field2);
source to share