- 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'; -- PostgreSQL
Modifying 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; -- PostgreSQL
Renaming 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 TABLE
to define a new table with appropriate constraints. - Use
ALTER TABLE
to modify existing tables. - Use
DROP TABLE
to remove a table completely. - Use
TRUNCATE TABLE
to delete all records but keep the table structure.
With these concepts, you can effectively create and manage tables in SQL.
Prepare for Interview
- 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
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
Random Blogs
- Data Analytics: The Power of Data-Driven Decision Making
- Top 15 Recommended SEO Tools
- Datasets for Exploratory Data Analysis for Beginners
- 5 Ways Use Jupyter Notebook Online Free of Cost
- How to Become a Good Data Scientist ?
- AI in Cybersecurity: The Future of Digital Protection
- Exploratory Data Analysis On Iris Dataset
- Big Data: The Future of Data-Driven Decision Making
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- Types of Numbers in Python
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- What Is SEO and Why Is It Important?
- Extract RGB Color From a Image Using CV2
- Python Challenging Programming Exercises Part 2
- Variable Assignment in Python
Datasets for Machine Learning
- 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
- Artificial Characters Dataset