We already know how to create a table with constraints:
CREATE TABLE users (
user_id INT PRIMARY KEY, -- Primary Key Constraint
username VARCHAR(50) UNIQUE, -- Unique Key Constraint
email VARCHAR(100) NOT NULL, -- Not Null Constraint
age INT CHECK (age >= 18), -- Check Constraint
registration_date DATE DEFAULT CURRENT_DATE -- Default Constraint
);
Now I insert data into this table.
INSERT INTO table_name (column1, column2, ..., columnN)
VALUES (value1, value2, ..., valueN);
Now I insert data into the table that I created already.
-- Create the table with constraints
CREATE TABLE users (
user_id INT PRIMARY KEY, -- Primary Key Constraint
username VARCHAR(50) UNIQUE, -- Unique Key Constraint
email VARCHAR(100) NOT NULL, -- Not Null Constraint
age INT CHECK (age >= 18), -- Check Constraint
registration_date DATE DEFAULT CURRENT_DATE -- Default Constraint
);
-- Insert data into the table
INSERT INTO users (user_id, username, email, age)
VALUES
(1, 'john_doe', '[email protected]', 25),
(2, 'jane_smith', '[email protected]', 30),
(3, 'alice_jones', '[email protected]', 22);
Hey congratulations, you just created a table in the database 🥳
.gif)