Back to articles
CA 39 - Create Tables

CA 39 - Create Tables

via Dev.to BeginnersSanthosh V

Today I practiced some basic SQL constraints. I kept it simple and tried different rules like primary key, unique, not null, check, and foreign key. 1. Students Table (Unique ID) CREATE TABLE students ( id INT PRIMARY KEY , name VARCHAR ( 50 ), age INT ); id is unique for every student 2. Employees Table (No Empty Name & Email) CREATE TABLE employees ( id INT PRIMARY KEY , name VARCHAR ( 50 ) NOT NULL , email VARCHAR ( 100 ) NOT NULL , phone_number VARCHAR ( 15 ) ); name & email must be filled phone is optional 3. Users Table (Unique Username & Email) CREATE TABLE users ( id INT PRIMARY KEY , username VARCHAR ( 50 ) UNIQUE , email VARCHAR ( 100 ) UNIQUE ); no duplicates allowed 4. Products Table (Valid Price & Stock) CREATE TABLE products ( id INT PRIMARY KEY , name VARCHAR ( 50 ), price INT CHECK ( price > 0 ), stock INT CHECK ( stock >= 0 ) ); price > 0 stock ≥ 0 5 . Orders Table ( Default + Timestamp ) CREATE TABLE orders ( id INT PRIMARY KEY , status VARCHAR ( 20 ) DEFAULT 'pending

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
7 views

Related Articles