
CREATE TABLES
Tables with constraints QN:1 Create a table called students where each student has an id, name, and age. Ensure that the id uniquely identifies each student. Query : CREATE TABLE students ( id SERIAL PRIMARY KEY , name TEXT , age INT ); id should uniquely identify each student we use PRIMARY KEY QN:2 Create a table employees where name and email cannot be empty, but phone_number can be optional. Query : CREATE TABLE employees ( id SERIAL PRIMARY KEY , name TEXT NOT NULL , email TEXT NOT NULL , phone_number TEXT ); name and email should not be empty we use NOT NULL phone is optional so no constraint QN:3 Create a table users where both username and email must be unique across all records. Query : CREATE TABLE users ( id SERIAL PRIMARY KEY , username TEXT UNIQUE , email TEXT UNIQUE ); username and email should not repeat we use UNIQUE QN:4 Create a table products where price must always be greater than 0 and stock cannot be negative. Query : CREATE TABLE products ( id SERIAL PRIMARY KEY
Continue reading on Dev.to Python
Opens in a new tab

