Introduction
A primary key helps to uniquely identify each row in a table so you can find or update data easily.
CREATE TABLE table_name ( column_name datatype PRIMARY KEY, other_column datatype );
CREATE TABLE Students ( StudentID INT PRIMARY KEY, Name VARCHAR(100) );
CREATE TABLE Employees ( EmployeeID INT, Name VARCHAR(100), PRIMARY KEY (EmployeeID) );
CREATE TABLE EmptyTable ( ID INT PRIMARY KEY );
CREATE TABLE Books ( BookID INT PRIMARY KEY, Title VARCHAR(255), Author VARCHAR(255) ); INSERT INTO Books (BookID, Title, Author) VALUES (1, 'The Hobbit', 'J.R.R. Tolkien'), (2, '1984', 'George Orwell'); SELECT * FROM Books;
PRIMARY KEY in a MySQL table?CREATE TABLE statement?PRIMARY KEY right after the column type, like id INT PRIMARY KEY.PRIMARY KEY inline after id INT. Others have syntax errors or misplaced keywords.id?
CREATE TABLE products ( id INT PRIMARY KEY, name VARCHAR(100) ); INSERT INTO products (id, name) VALUES (1, 'Pen'); INSERT INTO products (id, name) VALUES (1, 'Pencil');
id must have unique values. Duplicate values cause errors.id=1. The second insert tries to add the same id=1, violating uniqueness.CREATE TABLE orders ( order_id INT, customer_id INT, PRIMARY KEY order_id );
PRIMARY KEY (order_id).PRIMARY KEY order_id without parentheses, causing a syntax error.employees with a composite primary key consisting of department_id and employee_number. Which is the correct way to declare this primary key?PRIMARY KEY.Pass the test and this pattern moves unknown