0 day streak
sql / beginner
Snippet

Defining Unique Identifiers with Primary Keys

A PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values and cannot contain NULL values.

snippet.sql
sql
1
2
3
4
5
CREATE TABLE users (
user_id INT NOT NULL,
username VARCHAR(50) NOT NULL,
CONSTRAINT pk_users PRIMARY KEY (user_id)
);
Breakdown
1
CREATE TABLE users (
Starts the creation of a new table named users.
2
user_id INT NOT NULL,
Defines an integer column that cannot store NULL values.
3
username VARCHAR(50) NOT NULL,
Defines a variable-length character column up to 50 characters.
4
CONSTRAINT pk_users PRIMARY KEY (user_id)
Enforces unique values and non-nullability for user_id as the primary key.
5
);
Closes the table creation statement.