Introduction to SQL (Structured Query Language)
Introduction to SQL (Structured Query Language)
SQL is the backbone of relational database management. It allows users to interact with and manipulate data stored in databases effectively.
A Brief History of SQL
- Origin: SQL was developed in the 1970s by IBM during research on relational database technology.
- Name: It started as SEQUEL but was later renamed SQL (Structured Query Language) due to trademark reasons.
What is SQL?
SQL is a programming language designed to communicate with and manipulate databases. With SQL, users perform essential database tasks, commonly summarized as CRUD:
- C: Create
- R: Read
- U: Update
- D: Delete
How Does SQL Help?
SQL enables users to:
- Retrieve Data: Extract precise information using queries.
- Manipulate Data: Add, modify, or remove records in the database.
- Define Data: Create and modify the structure of databases (tables, views, indexes).
- Control Data: Manage who can access and alter the database by granting or revoking permissions.
Types of SQL Commands
SQL commands are typically grouped based on their functionality:
-
DQL (Data Query Language):
- Used to retrieve data from a database.
- Example:
SELECT
-
DML (Data Manipulation Language):
- Modifies data stored in the database.
- Examples:
INSERT,UPDATE,DELETE
-
DDL (Data Definition Language):
- Defines and modifies the structure of database schema.
- Examples:
CREATE,DROP,RENAME,TRUNCATE,ALTER
-
DCL (Data Control Language):
- Manages database permissions and security.
- Examples:
GRANT,REVOKE
-
TCL (Transaction Control Language):
- Controls database transactions.
- Examples:
COMMIT,ROLLBACK
Common SQL Commands and Syntax
Here are some basic SQL commands used for database operations:
1. Create a Database
CREATE DATABASE STUDENT;
2. Drop a Database
DROP DATABASE STUDENT;
3. Show All Databases
SHOW DATABASES;
4. Use a Database
USE STUDENT;
5. Create a Table
CREATE TABLE employee(
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
salary INT
);
6. Insert Data Into a Table
INSERT INTO employee(id, name, age, salary) VALUES
(1, 'rahul', 23, 23322),
(2, 'vivek', NULL, 30000),
(3, 'yogesh', NULL, 45000),
(4, 'ram', 27, 50000);
Note: If age is not provided, use NULL or specify values for consistency.
7. Select Data From a Table
SELECT * FROM employee;
SELECT name, salary FROM employee WHERE department = 'HR';
8. Update Existing Records
UPDATE employee SET salary = 50000 WHERE department = 'HR';
9. Alter Table Structure
ALTER TABLE employee ADD dob VARCHAR(20) DEFAULT 'NA';
ALTER TABLE employee DROP COLUMN dob;
10. Truncate a Table
TRUNCATE TABLE employee;
11. Drop a Table
DROP TABLE employee;
12. Delete Specific Records
DELETE FROM employee WHERE id = 3;
Note: Use
DELETEandUPDATEcommands carefully—they can change or remove large amounts of data if used without proper conditions!
Comments
Post a Comment