0% found this document useful (0 votes)
2 views

SQL Commands Classification

The document provides an overview of SQL commands, classifying them into five types: DDL, DML, DCL, TCL, and DQL, each serving different database operations. It also details MySQL constraints that ensure data integrity, including primary keys, foreign keys, unique constraints, and others. Additionally, the document outlines various MySQL data types, including numeric, string, and date/time types, along with their ranges and sizes.

Uploaded by

nidhaaalshalseer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL Commands Classification

The document provides an overview of SQL commands, classifying them into five types: DDL, DML, DCL, TCL, and DQL, each serving different database operations. It also details MySQL constraints that ensure data integrity, including primary keys, foreign keys, unique constraints, and others. Additionally, the document outlines various MySQL data types, including numeric, string, and date/time types, along with their ranges and sizes.

Uploaded by

nidhaaalshalseer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SQL COMMANDS CLASSIFICATION:

Commands are instructions or statements used to communicate with the


database.
Sql commands are used to perform various operations on the database like
create, retrieve, update and delete so on.

Sql commands classified into 5 types:


1. Data Definition Language(DDL)
2. Data Manipulation Language(DML)
3. Data Control Language(DCL)
4. Transaction Control Language(TCL)
5. Data Query Language(DQL)

DDL (Data Definition Language):

DDL statements define the structure of a database.


- CREATE: Create a database, table, or index.
Syntax: CREATE DATABASE database_name;
Example: CREATE DATABASE mydatabase;
Syntax: CREATE TABLE table_name (column1 data_type, column2 data_type);
Example: CREATE TABLE customers (id INT, name VARCHAR(255));

- ALTER: Modify table structure.


Syntax: ALTER TABLE table_name ADD COLUMN column_name data_type;
Example: ALTER TABLE customers ADD COLUMN email VARCHAR(255);
- DROP: Delete a database, table, or index.
Syntax: DROP DATABASE database_name;
Example: DROP DATABASE mydatabase;

Syntax: DROP TABLE table_name;


Example: DROP TABLE customers;

- TRUNCATE: Delete all data from a table.


Syntax: TRUNCATE TABLE table_name;
Example: TRUNCATE TABLE customers;

- RENAME: Rename a table.


Syntax: RENAME TABLE old_table_name TO new_table_name;
Example: RENAME TABLE customers TO clients;

DML (Data Manipulation Language)

DML statements manage data within database tables.


- INSERT: Add new data to a table.
Syntax: INSERT INTO table_name (column1, column2) VALUES ('value1',
'value2');
Example: INSERT INTO customers (id, name) VALUES (1, 'John Doe');

- UPDATE: Modify existing data.


Syntax: UPDATE table_name SET column1 = 'new_value' WHERE condition;
Example: UPDATE customers SET name = 'Jane Doe' WHERE id = 1;
- DELETE: Delete data.
Syntax: DELETE FROM table_name WHERE condition;
Example: DELETE FROM customers WHERE id = 1;

DCL (Data Control Language)

DCL statements control user access and permissions.


- GRANT: Assign privileges to a user.
Syntax: GRANT privilege ON database_name.* TO 'username'@'hostname';
Example: GRANT SELECT ON mydatabase.* TO 'john'@'%';

- REVOKE: Revoke privileges from a user.


Syntax: REVOKE privilege ON database_name.* FROM
'username'@'hostname';
Example: REVOKE SELECT ON mydatabase. * FROM 'john'@'%';

TCL (Transaction Control Language)

TCL statements manage database transactions.


- BEGIN: Start a transaction.
Syntax: BEGIN;
Example: BEGIN;

- COMMIT: To Save changes permanently.


Syntax: COMMIT;
Example: COMMIT;
- ROLLBACK: Undo changes.
Syntax: ROLLBACK;
Example: ROLLBACK;

- SAVEPOINT: Create a recovery point.


Syntax: SAVEPOINT point_name;
Example: SAVEPOINT my_savepoint;

DQL (Data Query Language)

DQL statements retrieve data from database tables.


- SELECT: Retrieve data.
Syntax: SELECT * FROM table_name;
Example: SELECT * FROM customers;

- SHOW: Display database information.


Syntax: SHOW DATABASES;
Example: SHOW DATABASES;

Syntax: SHOW TABLES;


Example: SHOW TABLES;
Syntax: SHOW COLUMNS FROM table_name;
Example: SHOW COLUMNS FROM customers;
MYSQL CONSTRAINTS:

MySQL constraints are rules that are applied to data in a table to ensure data
integrity and consistency. Here are the main types of MySQL constraints with
examples:
1. PRIMARY KEY (PK) CONSTRAINT
- Uniquely identifies each row in a table.
- Ensures no duplicate values.
Example:
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(255)
);

2. FOREIGN KEY (FK) CONSTRAINT


- Links two tables.
- Ensures referential integrity.

Example:
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
3. UNIQUE CONSTRAINT

- Ensures no duplicate values in a column.


Example:
CREATE TABLE customers (
id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE
);

4. NOT NULL CONSTRAINT


- Ensures a column cannot contain null values.
Example:
CREATE TABLE customers (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);

5. CHECK CONSTRAINT
- Ensures data meets specific conditions.

Example:
CREATE TABLE customers (
id INT PRIMARY KEY,
age INT CHECK (age >= 18)
);
6. DEFAULT CONSTRAINT

- Sets a default value for a column.

Example:
CREATE TABLE customers (
id INT PRIMARY KEY,
country VARCHAR(255) DEFAULT 'USA'
);

7. AUTO_INCREMENT CONSTRAINT

- Automatically increments a column value.

Example:
CREATE TABLE customers (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255)
);
MYSQL DATATYPES:

MySQL data types define the type of value that can be stored in a column.
Numeric Data Types
1. TINYINT
- Range: -128 to 127 (signed), 0 to 255 (unsigned)
- Size: 1 byte
2. SMALLINT
- Range: -32,768 to 32,767 (signed), 0 to 65,535 (unsigned)
- Size: 2 bytes
3. MEDIUMINT
- Range: -8,388,608 to 8,388,607 (signed), 0 to 16,777,215 (unsigned)
- Size: 3 bytes
4. INT
- Range: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295
(unsigned)
- Size: 4 bytes
5. BIGINT
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed),
0 to 18,446,744,073,709,551,615 (unsigned)
- Size: 8 bytes
6. FLOAT
- Range: -3.4E+38 to 3.4E+38
- Size: 4 bytes
7. DOUBLE
- Range: -1.8E+308 to 1.8E+308
- Size: 8 bytes
8. DECIMAL
- Range: -10^65 to 10^65
- Size: variable (dependent on precision and scale)

String Data Types

1. CHAR
- Range: 0 to 255 characters
- Size: fixed (dependent on length)

2. VARCHAR
- Range: 0 to 65,535 characters
- Size: variable (dependent on length)

3. TINYTEXT
- Range: 0 to 255 characters
- Size: variable (dependent on length)

4. TEXT
- Range: 0 to 65,535 characters
- Size: variable (dependent on length)

5. MEDIUMTEXT
- Range: 0 to 16,777,215 characters
- Size: variable (dependent on length)
6. LONGTEXT
- Range: 0 to 4,294,967,295 characters
- Size: variable (dependent on length)

Date and Time Data Types

1. DATE
- Range: 1000-01-01 to 9999-12-31
- Size: 3 bytes

2. TIME
- Range: -838:59:59 to 838:59:59
- Size: 3 bytes

3. DATETIME
- Range: 1000-01-01 00:00:00 to 9999-12-31 23:59:59
- Size: 5 bytes

4. TIMESTAMP
- Range: 1970-01-01 00:00:00 to 2038-01-19 03:14:07
- Size: 4 bytes

You might also like