However, if all the columns of the Composite Key are Foreign Keys in their own right, then the Composite Key is known as a Compound Key. Each Candidate Key (or column) that makes up a Composite Key may or may not be a Foreign Key.A Composite Key can be created by combining more than one Candidate Key.Features of Composite Keysįollowing are some important features of the SQL Composite Key − We can select the two columns AADHAAR_ID and MOBILE_NO and define a Composite key on them, and it can be used to fetch the records of the CUSTOMERS table uniquely. Suppose if we have a table named CUSTOMERS with various fields like ID, NAME, AGE, AADHAAR_ID, MOBILE_NO and SALARY as shown below − Let us understand the composite keys with an example. In such cases, we might need to use the combination of columns to ensure that each record in the table is distinct and identifiable. It can also be described as a Primary Key created on multiple columns.Ĭomposite Keys are necessary in scenarios where a database table does not have a single column that can uniquely identify each row from the table. This example we've created a primary key called contacts_pk that is made up of a combination of the last_name and first_name columns.An SQL Composite Key is a key that can be defined on two or more columns in a table to uniquely identify any record. We could also create a primary key with more than one field as in the example below: ALTER TABLE contacts In this example, we've created a primary key on the existing contacts table called contacts_pk. Let's look at an example of how to create a primary key using the ALTER TABLE statement in MySQL. column_n The columns that make up the primary key. constraint_name The name of the primary key. column_n) table_name The name of the table to modify. The syntax to create a primary key using the ALTER TABLE statement in MySQL is: ALTER TABLE table_name You can create a primary key in MySQL with the ALTER TABLE statement. So each combination of last_name and first_name must be unique in the contacts table.Ĭreate Primary Key - Using ALTER TABLE statement This example creates a primary key called contacts_pk that is made up of a combination of the last_name and first_name columns. We could also create a primary key with more than one field as in the example below: CREATE TABLE contactsĬONSTRAINT contacts_pk PRIMARY KEY (last_name, first_name) It consists of only one column - the contact_id column. In this example, we've created a primary key on the contacts table called contacts_pk. ( contact_id INT(11) NOT NULL AUTO_INCREMENT,ĬONSTRAINT contacts_pk PRIMARY KEY (contact_id) Let's look at an example of how to create a primary key using the CREATE TABLE statement in MySQL. See the MySQL CREATE TABLE statement for more detailed CREATE TABLE syntax as this is an over-simplification to demonstrate how to create a primary Key. column1, column2 The columns that you wish to create in the table. ) table_name The name of the table that you wish to create. The syntax to create a primary key using the CREATE TABLE statement in MySQL is: CREATE TABLE table_name You can create a primary key in MySQL with the CREATE TABLE statement. Create Primary Key - Using CREATE TABLE statement
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |