

In addition, a table can have multiple UNIQUE indexes. Unlike the PRIMARY index, MySQL allows NULL values in the UNIQUE index. You use the KEY when you want to create an index for a column or a set of columns that is not the part of a primary key or unique key.Ī UNIQUE index ensures that values in a column must be unique. The data in the column(s), which will be included in the primary key, must be unique and not NULL. If you add a primary key to a table that already has data.
Creating table in mysql code#
Second, add a primary key to the pkdemos table using the ALTER TABLE statement: ALTER TABLE pkdemosĪDD PRIMARY KEY( id) Code language: SQL (Structured Query Language) ( sql ) The following example adds the id column to the primary key.įirst, create the pkdemos table without a primary key. If a table, for some reasons, does not have a primary key, you can use the ALTER TABLEstatement to add a primary key to the table as follows: ALTER TABLE table_nameĪDD PRIMARY KEY(column_list) Code language: SQL (Structured Query Language) ( sql ) 2) Define PRIMARY KEY constraints using ALTER TABLE Note that the statement also created two foreign key constraints. It defines the PRIMARY KEY constraint as the table constraint: CREATE TABLE user_roles(

The following example creates the user_roles table whose primary key consists of two columns: user_id and role_id. You put a comma-separated list of primary key columns inside parentheses followed the PRIMARY KEY keywords. In case the primary key consists of multiple columns, you must specify them at the end of the CREATE TABLE statement. This statement creates the roles table that has the PRIMARY KEY constraint as the table constraint: CREATE TABLE roles( The following example creates a table named users whose primary key is the user_id column: CREATE TABLE users( The PRIMARY KEY table constraint can be used when the primary key has one column: CREATE TABLE table_name ( In this syntax, you separate columns in the column_list by commas (,). When the primary key has more than one column, you must use the PRIMARY KEY constraint as a table constraint. ) Code language: SQL (Structured Query Language) ( sql ) If the primary key has one column, you can use the PRIMARY KEY constraint as a column constraint: CREATE TABLE table_name( Typically, you define the primary key for a table in the CREATE TABLE statement. 1) Define a PRIMARY KEY constraint in CREATE TABLE The PRIMARY KEY constraint allows you to define a primary key of a table when you create or alter table. When you define a primary key for a table, MySQL automatically creates an index called PRIMARY. And you should ensure sure that value ranges of the integer type for the primary key are sufficient for storing all possible rows that the table may have.Ī primary key column often has the AUTO_INCREMENT attribute that automatically generates a sequential integer whenever you insert a new row into the table.

Because MySQL works faster with integers, the data type of the primary key column should be the integer e.g., INT, BIGINT.
