Primary Keys in SQL

We want all our data entries in a table to be uniquely identifiable. A Primary key is a unique identifier on a row. A good example of a primary key is your twitter handle. No 2 twitter handles are the same, and your twitter handle cannot be NULL. You c…


This content originally appeared on DEV Community and was authored by Jo

We want all our data entries in a table to be uniquely identifiable. A Primary key is a unique identifier on a row. A good example of a primary key is your twitter handle. No 2 twitter handles are the same, and your twitter handle cannot be NULL. You can change your twitter handle, but it always has to be unique (and twitter will not allow the change unless it is unique value).

CREATE TABLE unique_students (student_id INT NOT NULL,
name VARCHAR(100),
age INT,
PRIMARY KEY(student_id)
);

The below image will shows us what the above code creates in mysql:

Here we can see that the primary key is the student_id. However, it would be less efficient in this database to manually create our primary key. We can do this automatically with the auto_increment keyword.

CREATE TABLE unique_students (
student_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
age INT,
PRIMARY KEY(student_id)
);

The auto_increment id will add a 1 each time, so we no longer have to specify it when creating a new entry
In the highlighted section of the graphic below, we can see that we have 2 identical entries except they have different student_id's because they've been auto_incremented. They are actually 2 separate students who happen to have the same first name and age (which is quite common in a school setting).

The auto_increment now lies in the "Extra" field (use the DESC keyword to show the table's description.

That's all for this blog, folks. Thanks for checking it out :)


This content originally appeared on DEV Community and was authored by Jo


Print Share Comment Cite Upload Translate Updates
APA

Jo | Sciencx (2021-10-11T00:19:41+00:00) Primary Keys in SQL. Retrieved from https://www.scien.cx/2021/10/11/primary-keys-in-sql/

MLA
" » Primary Keys in SQL." Jo | Sciencx - Monday October 11, 2021, https://www.scien.cx/2021/10/11/primary-keys-in-sql/
HARVARD
Jo | Sciencx Monday October 11, 2021 » Primary Keys in SQL., viewed ,<https://www.scien.cx/2021/10/11/primary-keys-in-sql/>
VANCOUVER
Jo | Sciencx - » Primary Keys in SQL. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/11/primary-keys-in-sql/
CHICAGO
" » Primary Keys in SQL." Jo | Sciencx - Accessed . https://www.scien.cx/2021/10/11/primary-keys-in-sql/
IEEE
" » Primary Keys in SQL." Jo | Sciencx [Online]. Available: https://www.scien.cx/2021/10/11/primary-keys-in-sql/. [Accessed: ]
rf:citation
» Primary Keys in SQL | Jo | Sciencx | https://www.scien.cx/2021/10/11/primary-keys-in-sql/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.