Introduction to SQL
SQL (Structured Query Language) is a standard programming language specifically designed for managing and manipulating relational databases. It is used to perform tasks such as querying data, updating records, inserting data, and deleting records. Here are some key points about SQL:
Data Querying: SQL allows users to query data from a database using the
SELECT
statement, which can retrieve specific data based on given criteria.Data Manipulation: SQL provides commands for inserting (
INSERT
), updating (UPDATE
), and deleting (DELETE
) data within database tables.Data Definition: SQL includes commands for defining the structure of databases and tables, such as
CREATE TABLE
,ALTER TABLE
, andDROP TABLE
.Data Control: SQL has features for controlling access to data within a database, including granting and revoking permissions using
GRANT
andREVOKE
.
Keep in Mind That...SQL keywords are NOT case sensitive: select is the same as SELECT
Semicolon after SQL Statements?
Some of The Most Important SQL Commands
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
UPDATE - updates data in a table
DELETE - deletes data from a table
INSERT INTO - inserts new data into a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
Creating a Database
The CREATE DATABASE command is used to create a new database.
CREATE DATABASE database_name;
Altering a Database
ALTER DATABASE old_database_name RENAME TO new_database_name;
Example: Changing the Owner of a Database (PostgreSQL)
ALTER DATABASE database_name OWNER TO new_owner;
DROP DATABASE database_name;
CREATE TABLE: Used to create a new table.
CREATE TABLE table_name
ALTER TABLE: Used to modify an existing table (e.g., adding or dropping columns)
DROP TABLE: Used to delete a table
DROP TABLE table_name;
CREATE INDEX: Used to create an index (search key).
CREATE INDEX index_name ON table_name (column1, column2);
2.Data Manipulation Commands
INSERT INTO: Used to insert new records into a table.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
UPDATE: Used to modify existing records in a table.
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
DELETE: Used to delete records from a table.
DELETE FROM table_name WHERE condition;
SELECT: Used to query data from a database.
SELECT column1, column2 FROM table_name;
WHERE: Used to filter records.
SELECT column1, column2 FROM table_name WHERE condition;
ORDER BY: Used to sort the result set.
SELECT column1, column2 FROM table_name ORDER BY column1 [ASC|DESC];
JOIN: Used to combine rows from two or more tables based on a related column.
GRANT: Used to give user access privileges to the database.
GRANT SELECT, INSERT ON table_name TO 'username';
REVOKE: Used to remove user access privileges.
REVOKE SELECT, INSERT ON table_name FROM 'username';
4.Transaction Control Commands
BEGIN TRANSACTION: Used to start a transaction.
BEGIN TRANSACTION;
COMMIT: Used to save the changes made by the transaction.
COMMIT;
ROLLBACK: Used to undo changes made by the transaction.
ROLLBACK;
Comments
Post a Comment