SQL (Structured Query Language) is a standard programming language specifically designed for managing and manipulating databases. SQL allows you to create, read, update, and delete (CRUD) data stored in a relational database.
The CREATE DATABASE
statement is used to create a new database.
CREATE DATABASE company_db;
To start working with a specific database, use the USE
statement.
USE company_db;
The SHOW TABLES
statement is used to display all tables in the current database.
SHOW TABLES;
The CREATE TABLE
statement is used to create a new table in the database.
CREATE TABLE employees (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50)
);
The DESCRIBE
statement is used to display the structure of a table.
DESCRIBE employees;
The DROP TABLE
statement is used to delete an existing table and its data.
DROP TABLE employees;
The SELECT
statement is used to retrieve data from a database.
SELECT column1, column2, ...
FROM table_name;
Example:
SELECT first_name, last_name
FROM employees;
The WHERE
clause is used to filter records that meet certain conditions.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
SELECT first_name, last_name
FROM employees
WHERE department = 'Sales';
The INSERT INTO
statement is used to add new records to a table.
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
INSERT INTO employees (first_name, last_name, department)
VALUES ('John', 'Doe', 'Sales');
The UPDATE
statement is used to modify existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE employees
SET department = 'Marketing'
WHERE last_name = 'Doe';
The DELETE
statement is used to delete records from a table.
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM employees
WHERE last_name = 'Doe';
The CREATE VIEW
statement is used to create a view. A view is a virtual table based on the result set of an SQL statement.
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
CREATE VIEW sales_employees AS
SELECT first_name, last_name
FROM employees
WHERE department = 'Sales';
You can query a view just like a regular table.
SELECT column1, column2, ...
FROM view_name;
Example:
SELECT first_name, last_name
FROM sales_employees;
The DROP VIEW
statement is used to delete a view.
DROP VIEW view_name;
Example:
DROP VIEW sales_employees;
This documentation provides a basic overview of SQL and its fundamental operations. SQL is a powerful language for managing databases, and mastering its basics is essential for working with relational databases.