A SQL primary key could be a column (or combination of columns) in a table that uniquely recognizes each row. It may be a special type of constraint, and is the most imperative characteristic of any relational database table. Primary keys must contain unique values and cannot contain null values. They are used to reference a row in another table, form relationships, and enforce data integrity.
The primary key's main advantage is ensuring data integrity by uniquely identifying each row in a table. This helps to prevent duplicate records from being created and ensures that each record can easily be retrieved and updated. Primary keys also help improve queries' performance as they allow for faster searching and indexing.
ALTER TABLE table_name
ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY;
This can be an SQL statement utilized to make a primary key for a single column in a table. The statement includes a column named "id" of type INT (integer) and sets it as the primary key, meaning it'll be utilized to identify each row within the table interestingly. The AUTO_INCREMENT keyword causes the column's value to increment automatically each time a new row is inserted into the table.
CREATE TABLE students (
student_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
age INT
);
This statement creates a new " students " table with four columns: student_id, first_name, last_name, and age. The student_id column is set to be the PRIMARY KEY, meaning that it must contain a unique value for each row in the table.
CREATE TABLE table_name (
column1 INT NOT NULL,
column2 INT NOT NULL,
column3 INT NOT NULL,
PRIMARY KEY (column1, column2, column3)
);
This creates a table with three columns, all of which are integers and are not allowed to be null. It then creates a primary key constraint on all three columns, ensuring that all the values in the three columns together are unique.
CREATE TABLE students (
student_id INT NOT NULL,
class INT NOT NULL,
period INT NOT NULL,
PRIMARY KEY (student_id, class, period)
);
This creates a table of students, with each row containing the student's ID, the class they are in, and the period of the day. The primary key ensures that each student is unique for a given class and period.
Create Table in pgAdmin
Create Table Students
Define Columns and set Primary Key
If the primary key is not set when defining the column, you can set it using Constraints.
Set Primary Key in Constraints
Verify table Creation with Primary Key
Use the Query Tool in pgAdmin to run SQL commands directly
Query Tool in pgAdmin
SQL Query to Create the students Table:
CREATE TABLE students ( roll_no INT NOT NULL PRIMARY KEY, student_name VARCHAR(100) NOT NULL, age INT, course VARCHAR(50), email VARCHAR(100) ); |
Inserting Data into the students Table:
INSERT INTO students (student_name, age, course, email) VALUES ('Amit Sharma', 20, 'Computer Science', 'amit.sharma@example.com'), ('Priya Singh', 21, 'Mechanical Engineering', 'priya.singh@example.com'), ('Raj Patel', 22, 'Electrical Engineering', 'raj.patel@example.com'), ('Sneha Iyer', 19, 'Civil Engineering', 'sneha.iyer@example.com'), ('Vikram Rao', 23, 'Data Science', 'vikram.rao@example.com'); |
Querying the students Table:
SELECT * FROM students; |
Output:
create table and pk using code
Creating a PRIMARY KEY in SQL can vary slightly depending on the SQL database tool or system that is being used (e.g., MySQL, PostgreSQL, SQL Server, SQLite). Below are examples of how to define a PRIMARY KEY during a CREATE TABLE statement for different SQL systems:
In MySQL, you can define a PRIMARY KEY either inline (within the column definition) or after defining all columns.
CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, username VARCHAR(100) NOT NULL, email VARCHAR(100), PRIMARY KEY (id) ); |
In PostgreSQL, the syntax is similar to MySQL. You can define a PRIMARY KEY either inline with the column or as a separate constraint.
CREATE TABLE users ( id SERIAL PRIMARY KEY, username VARCHAR(100) NOT NULL, email VARCHAR(100) ); |
In SQL Server, you define the PRIMARY KEY using the same general syntax, but you may use IDENTITY for auto-increment behavior.
CREATE TABLE users ( id INT IDENTITY(1,1) PRIMARY KEY, username VARCHAR(100) NOT NULL, email VARCHAR(100) ); |
In SQLite, you define a PRIMARY KEY within the column definition. SQLite supports the AUTOINCREMENT keyword for primary keys.
CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, email TEXT ); |
In Oracle, the PRIMARY KEY is defined either inline or after defining all the columns. Oracle also supports sequences to generate unique values instead of auto-increment.
CREATE TABLE users ( id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, username VARCHAR2(100) NOT NULL, email VARCHAR2(100) ); |
To create a PRIMARY KEY based on multiple columns (composite key), you define it at the end of the CREATE TABLE statement:
CREATE TABLE orders ( order_id INT, product_id INT, quantity INT, PRIMARY KEY (order_id, product_id) ); |
Each SQL dialect may have small variations, but the general pattern of defining a primary key remains similar.
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column_name);
This query is utilized to include a primary key to an existing table. The 'table_name' indicates the name of the table to which the primary key will be included. The 'constraint_name' indicates the title of the constraint to be included. The 'column_name' indicates the column's title on which the primary key will be included.
ALTER TABLE orders
ADD CONSTRAINT orders_pk PRIMARY KEY (orderID);
This query is used to add a primary key to the 'orders' table. The primary key is named 'orders_pk' and is applied to the 'orderID' column. This primary key will ensure that no two orders have the same orderID.
ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY(column1, column2);
This statement adds a primary key constraint to a table and specifies multiple columns that should be part of the primary key. The constraint_name is an optional name for the constraint. The columns listed in the parentheses are the columns that will be part of the primary key.
ALTER TABLE students ADD CONSTRAINT student_pk PRIMARY KEY(student_id, student_name);
This statement adds a primary key constraint named "student_pk" to the table "students" and specifies that the columns "student_id" and "student_name" should be part of the primary key. This means that each combination of values for these two columns must be unique in the table.
To drop a primary key constraint, use the following SQL syntax:
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
For example:
ALTER TABLE student_table DROP CONSTRAINT pk_student_table_id;
This SQL statement will drop the primary key constraint on the student_table, identified as pk_student_table_id. After executing this statement, the primary key constraint will no longer be enforced on the student_table.
A primary key in SQL may be a column or combination of columns in a table that interestingly distinguishes each row. It implements data integrity, prevents duplicate records, and permits speedier searching and indexing. An essential key must contain one-of-a-kind and non-null values and be steady over time and can be made utilizing the Alter TABLE or Create TABLE statement. It can be dropped using the ALTER TABLE statement as well.
1.What is a primary key in SQL?
Answer: a. A unique identfier for each row in a table
2.What type of data must a primary key in SQL contain?
Answer: a. A unique value
3. What is the purpose of using a primary key in SQL?
Answer: b. To identify rows in a table
4. What is the syntax for creating a primary key in SQL?
Answer: d. ALTER TABLE table_name PRIMARY KEY
Top Tutorials
Related Articles