SQL (Structured Query Language) is the standard language used to interact with relational databases. It provides a powerful and flexible way to manage data, from creating and modifying database structures to querying and updating data.
SQL commands are typically divided into two main categories:
Data Definition Language (DDL) Commands: These commands are used to define the structure of a database, including creating, modifying, and deleting tables, columns, and indexes.
Data Manipulation Language (DML) Commands: These commands are used to manipulate data within a database, such as inserting, updating, deleting, and selecting data.
Basic SQL Commands
Here are some fundamental SQL commands to get you started:
CREATE TABLE: Used to create a new table in the database.
INSERT INTO: Used to insert new rows (records) into a table.
SELECT: Used to retrieve data from a table.
UPDATE: Used to modify existing data in a table.
DELETE: Used to delete data from a table.
Data Definition Language (DDL) Commands
DDL commands are used to define and modify the structure of a database. Some common DDL commands include:
CREATE: Creates database objects like tables, views, indexes, etc.
ALTER: Modifies existing database objects.
DROP: Deletes database objects.
TRUNCATE: Deletes all data from a table.
Data Manipulation Language (DML) Commands
DML commands are used to manipulate data within a database. Some common DML commands include:
SELECT: Retrieves data from a table.
INSERT: Inserts new rows into a table.
UPDATE: Modifies existing data in a table.
DELETE: Deletes data from a table.
Example: Creating a Table and Inserting Data
-- Create a table named "customers"
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
customer_name VARCHAR(50),
email VARCHAR(100)
);
-- Insert data into the "customers" table
INSERT INTO customers (customer_id, customer_name, email)
VALUES (1, 'John Doe', 'johndoe@example.com');
-- Selecting Records from "customers" table
SELECT customer_id, customer_name FROM customers WHERE city = 'New York';
Note: The specific syntax of SQL commands can vary slightly depending on the database system you're using (e.g., MySQL, PostgreSQL, Oracle).
Additional SQL Concepts
Joins: Combine data from multiple tables.
Aggregates: Calculate summary statistics (e.g., SUM, AVG, COUNT).
Subqueries: Nested queries within a main query.
Views: Virtual tables derived from other tables.
Stored Procedures: Predefined sets of SQL statements.
By mastering these fundamental SQL concepts and commands, you'll be well-equipped to work with databases and manage your data effectively.
Comments