Welcome to SQL Task 2 of my Elevate Labs internship! This project demonstrates my understanding of advanced SQL table design, focusing on data types, constraints, and structural modifications to maintain data integrity.
The primary goal of this task is to:
- Define Schema: Choose appropriate data types (
INT,VARCHAR,DATE) for different fields. - Apply Constraints: Implement
PRIMARY KEY,NOT NULL,UNIQUE, andCHECKconstraints to ensure robust data validation. - Test Integrity: Perform valid and invalid data insertions to observe constraint behavior.
- Modify Structure: Use
ALTER TABLEto add, rename, and drop columns dynamically.
To successfully run the scripts in this project, ensure you have the following:
- SQL Server: MySQL, MariaDB, or any compatible SQL engine.
- SQL Client: A command-line interface (CLI) or a graphical tool like MySQL Workbench.
The project expands upon the students table design to enforce strict data rules.
| Column Name | Data Type | Constraint | Description |
|---|---|---|---|
🔑 id |
INT |
PRIMARY KEY, AUTO_INCREMENT |
Unique identifier for each student |
👤 name |
VARCHAR(50) |
NOT NULL |
Full name of the student |
📧 email |
VARCHAR(100) |
UNIQUE, NOT NULL |
Unique email address |
📅 dob |
DATE |
NOT NULL |
Date of birth |
🔢 age |
INT |
CHECK (age > 0) |
Verified positive age |
Follow these steps to initialize and test the database:
- Start Server: Ensure your SQL server instance is running.
- Connect: Use your preferred SQL client to connect to the server.
- Execute Script: Open and run the
task2.sqlfile.source path/to/task2.sql;
- Verify: Check the output to observe constraint successes and failures, as well as the table alterations.
The task2.sql script includes several essential SQL operations:
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL,
dob DATE NOT NULL,
age INT CHECK (age > 0)
);-- Valid Insert
INSERT INTO students (name, email, dob, age) VALUES ('Arun Kumar', 'arun@gmail.com', '2003-05-10', 21);
-- Invalid Insert (Fails UNIQUE constraint on email)
INSERT INTO students (name, email, dob, age) VALUES ('Test User', 'arun@gmail.com', '2000-01-01', 20);-- Add a new column
ALTER TABLE students ADD phone_number VARCHAR(15);
-- Rename a column
ALTER TABLE students RENAME COLUMN phone_number TO mobile_number;
-- Drop a column
ALTER TABLE students DROP COLUMN age;Tip
When running the script, you will intentionally encounter errors during the invalid data insertion phase. These errors prove that your constraints are working properly to protect the database!