- 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
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- The Ultimate Guide to Data Science: Everything You Need to Know
- Types of Numbers in Python
- Government Datasets from 50 Countries for Machine Learning Training
- Transforming Logistics: The Power of AI in Supply Chain Management
- Datasets for analyze in Tableau
- AI is Replacing Search Engines: The Future of Online Search
- The Ultimate Guide to Starting a Career in Computer Vision
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- Downlaod Youtube Video in Any Format Using Python Pytube Library
- Understanding HTAP Databases: Bridging Transactions and Analytics
- Convert RBG Image to Gray Scale Image Using CV2
- AI in Cybersecurity: The Future of Digital Protection
- The Beginner’s Guide to Normalization and Denormalization in Databases
- Understanding Data Lake, Data Warehouse, Data Mart, and Data Lakehouse – And Why We Need Them
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

