SQL Commands Classification
SQL Commands Classification
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)
);
Example:
CREATE TABLE orders (
id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);
3. UNIQUE CONSTRAINT
5. CHECK CONSTRAINT
- Ensures data meets specific conditions.
Example:
CREATE TABLE customers (
id INT PRIMARY KEY,
age INT CHECK (age >= 18)
);
6. DEFAULT CONSTRAINT
Example:
CREATE TABLE customers (
id INT PRIMARY KEY,
country VARCHAR(255) DEFAULT 'USA'
);
7. AUTO_INCREMENT CONSTRAINT
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)
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)
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