+ 1

What is the use of Primary key

18th Dec 2018, 6:34 AM
Sanjay Suresh
2 Réponses
+ 1
Think of it this way: When you order something online, say on Amazon or on Flipkart, you get an Order ID. This Order ID can be only related to you and no one else's order. This ID here identifies your order uniquely, thus making it the Primary Key
19th Dec 2018, 7:31 AM
Kashyap Maheshwari
+ 2
PRIMARY KEY is a constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values. For example if you have a table 'Customers' you should have a key in order to identify each record. You can have an ID as PRIMARY KEY for each customer. -- ----------------------------------------- --HOW TO CREATE PRIMARY KEY - -- ----------------------------------------- CREATE TABLE CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); As a result is forbidden the following data : -------------------------------------------------- | ID | NAME | AGE | ADDRESS | SALARY | -------------------------------------------------- | 1 | George | 24 | City 40 | 1200 | | 1 | Mika | 23 | City 10 | 1600 | -------------------------------------------------- But it is allowed : -------------------------------------------------- | ID | NAME | AGE | ADDRESS | SALARY | -------------------------------------------------- | 1 | George | 24 | City 40 | 1200 | | 2 | Mika | 23 | City 10 | 1600 | -------------------------------------------------- In more practical level, when you define a primary key in a where condition only one row should be returned. SELECT * FROM CUSTOMERS WHERE ID = 1 ; -- The first row should be returned ps : sorry for my bad english
18th Dec 2018, 6:56 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar