![]() That’s all you need to do to create a primary key. MySQL creates the unique index at the same time. The column’s definition includes the PRIMARY KEY keywords, which tells MySQL to create a PRIMARY KEY constraint on that column when creating the airplanes table. The statement creates a primary key on the plane_id column. The easiest way to add a single-column primary key is to include it in the column definition, as in the following example: You can add a primary key to a table when you create the table. ![]() This is important because a unique index on its own can contain multiple NULL values, so NOT NULL is needed to guarantee that a unique identifier is associated with each row. If they are not explicitly defined as NOT NULL, MySQL implicitly declares them as such to ensure that each row in the table has an associated primary key value. When you add a PRIMARY KEY constraint to a table, MySQL also creates a unique index on the key columns to enforce their uniqueness.Ī table can have only one primary key, and the key columns must be defined as NOT NULL. It is defined on one or more of a table’s columns (the key columns) to ensure the row’s uniqueness. ![]() PRIMARY KEY constraintsĪ PRIMARY KEY constraint provides a mechanism for uniquely identifying each row in a table. The last section of the article-“Appendix: Preparing your MySQL environment”-provides information about how I set up my environment and includes a SQL script for creating the database I used when building these examples. Note: The examples in this article are based on a local instance of MySQL that hosts a very simple database. For information about data types, see the MySQL article Data Types. I do not cover data types in this article and instead focus only the six other constraint types. For example, a column configured with an integer data type will take whole numbers but not decimals or string values. Some resources also consider data types to be constraints because they limit the types of data that can be inserted into a table’s columns. If you’re not familiar with the CREATE TABLE statement or how to create tables in a MySQL database, refer to an earlier article in this series that introduces you to the statement. The examples include a series of CREATE TABLE statements that demonstrate different ways to incorporate constraints into your table definitions. In this article, I introduce you to each constraint type and provide examples for how they work. MySQL supports six basic types of constraints for ensuring data integrity: PRIMARY KEY, NOT NULL, DEFAULT, CHECK, UNIQUE, and FOREIGN KEY. For example, you can use constraints to set a column’s uniqueness or nullability, specify a default value for a column, or verify that the data falls within a certain range of acceptable values. The constraints let you better control the types of data that can be added to the database. MySQL provides a set of constraints that you can include in your table definitions to help ensure the integrity of the data.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |