- SQL Basics
-
Overview
- Introduction to Databases and SQL
- Creating and Managing Tables in SQL
- Data Types and Constraints
- SELECT Queries and Filtering Data
- GROUP BY, HAVING, and ORDER BY
- SQL Joins (INNER, LEFT, RIGHT, FULL)
- Subqueries and Nested Queries
- UNION, INTERSECT, and EXCEPT
- Common Table Expressions (CTE)
- SQL Views and Stored Procedures
Creating and Managing Tables in SQL
Add to BookmarkIntroduction
Tables are the foundation of any relational database. They store structured data in rows and columns, defining the schema of a database. In this tutorial, we will cover how to create, modify, and manage tables in SQL using various SQL commands.
Creating a Table
To create a table in SQL, use the CREATE TABLE statement. A table consists of columns with defined data types and constraints.
Syntax:
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);Example:
Creating a students table:
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
age INT CHECK (age >= 18),
email VARCHAR(100) UNIQUE,
enrollment_date DATE DEFAULT CURRENT_DATE
);Explanation:
student_id INT PRIMARY KEY: Unique identifier for each student.
name VARCHAR(50) NOT NULL: Name cannot be empty.
age INT CHECK (age >= 18): Ensures students are at least 18 years old.
email VARCHAR(100) UNIQUE: No two students can have the same email.
enrollment_date DATE DEFAULT CURRENT_DATE: Defaults to the current date if not provided.
Viewing Table Structure
To check the structure of a table:
DESCRIBE students; -- MySQL
PRAGMA table_info(students); -- SQLite
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'students'; -- PostgreSQLModifying a Table
You can modify a table using the ALTER TABLE statement.
Adding a Column:
ALTER TABLE students ADD COLUMN phone VARCHAR(15);Modifying a Column:
ALTER TABLE students ALTER COLUMN age SET DEFAULT 20; -- PostgreSQLRenaming a Column:
ALTER TABLE students RENAME COLUMN phone TO contact_number;Deleting a Column:
ALTER TABLE students DROP COLUMN contact_number;Deleting a Table
If you need to remove a table, use the DROP TABLE statement.
DROP TABLE students;Truncating a Table
To delete all records without removing the structure:
TRUNCATE TABLE students;Summary
- Use
CREATE TABLEto define a new table with appropriate constraints. - Use
ALTER TABLEto modify existing tables. - Use
DROP TABLEto remove a table completely. - Use
TRUNCATE TABLEto delete all records but keep the table structure.
With these concepts, you can effectively create and manage tables in SQL.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Avoiding the Beginner’s Trap: Key Python Fundamentals You Shouldn't Skip
- Google’s Core Update in May 2020: What You Need to Know
- Big Data: The Future of Data-Driven Decision Making
- The Beginner’s Guide to Normalization and Denormalization in Databases
- Generative AI - The Future of Artificial Intelligence
- Exploratory Data Analysis On Iris Dataset
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
- Compiler SQL Online: A Beginner-Friendly Guide to Running SQL Queries Anywhere
- Why to learn Digital Marketing?
- Store Data Into CSV File Using Python Tkinter GUI Library
- Datasets for Exploratory Data Analysis for Beginners
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- String Operations in Python
- What is YII? and How to Install it?
- Transforming Logistics: The Power of AI in Supply Chain Management
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset

