![]() ![]() 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |