Back to articles
Creating Table

Creating Table

via Dev.to BeginnersAbirami Prabhakar

1. Create a table called students where each student has an id, name, and age. Ensure that the id uniquely identifies each student. CREATE TABLE students ( id SERIAL PRIMARY KEY, name VARCHAR(100), age INT ); 2. Create a table employees where name and email cannot be empty, but phone_number can be optional. NOT NULL contraint is used to ensure that a attribute cannot be left empty CREATE TABLE employees ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL, phone_number VARCHAR(15) ); 3. Create a table users where both username and email must be unique across all records. UNIQUE constaint is allow only unique values and no repeated values CREATE TABLE users ( id SERIAL PRIMARY KEY, username VARCHAR(50) UNIQUE, email VARCHAR(100) UNIQUE ); * 4. Create a table products where price must always be greater than 0 and stock cannot be negative. * DECIMAL datatype is used to keep records of money in detail and arthmetic operators are used to check certain values CREA

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
6 views

Related Articles