Creating Databases, Tables, Constraints, and Keys | SQL Tutorial - Learn with VOKS
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL
Back Next

Creating Databases, Tables, Constraints, and Keys

Learn this topic step-by-step with VOKS Tutorials.

1️⃣ How to Create a Database

To create a new database in SQL:

CREATE DATABASE CompanyDB;

To use that database:

USE CompanyDB;

2️⃣ SQL Constraints

Constraints are rules applied to table columns to ensure data integrity.

| Constraint  | Purpose                               | Example                                              |
|------------|---------------------------------------|------------------------------------------------------|
| NOT NULL    | Column must have a value              | Name VARCHAR(50) NOT NULL                            |
| UNIQUE      | Column values must be unique          | Email VARCHAR(100) UNIQUE                            |
| PRIMARY KEY | Uniquely identifies each row          | EmployeeID INT PRIMARY KEY                           |
| FOREIGN KEY | Links column to another table         | DeptID INT FOREIGN KEY REFERENCES Department(DeptID) |
| CHECK       | Ensures value meets a condition       | Salary INT CHECK(Salary>0)                           |
| DEFAULT     | Sets a default value if none provided | JoinDate DATE DEFAULT GETDATE()                      |

3️⃣ Creating Tables

To create a table with constraints:

CREATE TABLE Department (
    DeptID INT PRIMARY KEY,
    DeptName VARCHAR(50) NOT NULL UNIQUE
);

CREATE TABLE Employee (
    EmployeeID INT PRIMARY KEY,
    Name VARCHAR(50) NOT NULL,
    DepartmentID INT,
    Salary INT CHECK(Salary>0),
    JoinDate DATE DEFAULT GETDATE(),
    FOREIGN KEY (DepartmentID) REFERENCES Department(DeptID)
);
  • PRIMARY KEY ensures each record is unique
  • FOREIGN KEY links Employee.DepartmentID to Department.DeptID
  • CHECK ensures salary > 0
  • DEFAULT sets a default date

4️⃣ Altering Tables

You can modify existing tables using ALTER TABLE:

Add a Column

ALTER TABLE Employee
ADD Email VARCHAR(100) UNIQUE;

Modify a Column

ALTER TABLE Employee
MODIFY Salary INT NOT NULL;

Drop a Column

ALTER TABLE Employee
DROP COLUMN Email;

5️⃣ Dropping Tables

Remove a table using DROP TABLE:

DROP TABLE Employee;
DROP TABLE Department;

⚠️ Be careful: Dropping a table deletes all data permanently.

6️⃣ Primary Key

  • Ensures each row in the table is unique
  • Usually an ID column
  • Automatically NOT NULL

Example:

CREATE TABLE Department (
    DeptID INT PRIMARY KEY,
    DeptName VARCHAR(50) NOT NULL
);

7️⃣ Foreign Key

  • Links a column to a primary key in another table
  • Maintains referential integrity

Example:

CREATE TABLE Employee (
    EmployeeID INT PRIMARY KEY,
    Name VARCHAR(50) NOT NULL,
    DepartmentID INT,
    FOREIGN KEY (DepartmentID) REFERENCES Department(DeptID)
);
  • Cannot insert an Employee with a DepartmentID that doesn’t exist in the Department table

8️⃣ Beginner Example: Complete Setup

-- Create database
CREATE DATABASE CompanyDB;
USE CompanyDB;

-- Create Department Table
CREATE TABLE Department (
    DeptID INT PRIMARY KEY,
    DeptName VARCHAR(50) NOT NULL UNIQUE
);

-- Create Employee Table
CREATE TABLE Employee (
    EmployeeID INT PRIMARY KEY,
    Name VARCHAR(50) NOT NULL,
    DepartmentID INT,
    Salary INT CHECK(Salary>0),
    JoinDate DATE DEFAULT GETDATE(),
    FOREIGN KEY (DepartmentID) REFERENCES Department(DeptID)
);


QUICK KNOWLEDGE CHECK

Test Your Understanding

Answer 5 questions generated from this lesson. Your result is calculated instantly and is not saved.

0 / 5 answered
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL
Course contents
SQL
01 Introduction 02 What is a Database, Advantages of Database, and Database Tables 03 Creating Databases, Tables, Constraints, and Keys 04 Defining Data Types, Unique ID, Inserting Values, and Handling NULL 05 SELECT, DISTINCT, TOP, LIMIT, UPDATE, DELETE, ORDER BY, WHERE, HAVING, AND, OR, NOT 06 Aggregate function; Min, Max, Count, Avg, Sum 07 Wildcards, IN, AS, LIKE, BETWEEN, and Aliases 08 SQL Joins: Inner, Outer, Left, Right, Full, Cross 09 Set Theory for SQL: Joins, UNION, INTERSECT, EXCEPT, GROUP BY 10 SQL Arithmetic Operators 11 SQL Bitwise Operators 12 SQL Comparison Operator 13 SQL View/Null Functions 14 SQL Comments 15 SQL Case