0% found this document useful (0 votes)
36 views11 pages

MySQL Key Constraints Explained

The document outlines key MySQL constraints and commands, including DESC, DISTINCT, PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, DEFAULT, and ALTER TABLE. Each section provides definitions, syntax, and examples for creating and modifying tables. These commands are essential for managing data integrity and structure in MySQL databases.

Uploaded by

animisham911
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views11 pages

MySQL Key Constraints Explained

The document outlines key MySQL constraints and commands, including DESC, DISTINCT, PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, DEFAULT, and ALTER TABLE. Each section provides definitions, syntax, and examples for creating and modifying tables. These commands are essential for managing data integrity and structure in MySQL databases.

Uploaded by

animisham911
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

MySQL Key Constraints and

Commands
DESC, DISTINCT, FOREIGN KEY,
PRIMARY KEY, UNIQUE, CHECK,
DEFAULT, ALTER TABLE
DESC Command
Definition:
The DESC (DESCRIBE) command displays the
structure of a table.

Syntax:
DESC table_name;

Example:
DESC Employee;
DISTINCT Keyword
Definition:
The DISTINCT keyword removes duplicate rows from
the result set.

Syntax:
SELECT DISTINCT column_name FROM table_name;

Example:
SELECT DISTINCT EmpZone FROM Employee;
PRIMARY KEY
Definition:
A PRIMARY KEY uniquely identifies each record in a table.
- It must contain unique values.
- It cannot contain NULL values.

Example:
CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
FirstName VARCHAR(255),
LastName VARCHAR(255)
);
FOREIGN KEY
Definition:
A FOREIGN KEY in one table refers to the PRIMARY KEY in another table.
- Child Table: Table containing the foreign key.
- Parent Table: Table with the primary key.

Example:
CREATE TABLE Department (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(255),
EmpID INT,
FOREIGN KEY (EmpID) REFERENCES Employee(EmpID)
);
UNIQUE Constraint
Definition:
Ensures all values in a column are unique.

Example:
CREATE TABLE Student (
RollNo INT UNIQUE,
Name VARCHAR(255)
);
CHECK Constraint
Definition:
The CHECK constraint ensures that all values in a
column satisfy a specific condition.

Example:
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
Quantity INT CHECK (Quantity > 0)
);
DEFAULT Constraint
Definition:
The DEFAULT constraint provides a default value when no
value is supplied.

Example:
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(255),
Status VARCHAR(255) DEFAULT 'Available'
);
ALTER TABLE - Add Column
Definition:
Adds a new column to an existing table.

Syntax:
ALTER TABLE table_name ADD column_name
datatype;

Example:
ALTER TABLE Employee ADD EmpSalary INT;
ALTER TABLE - Drop Column
Definition:
Removes an existing column from a table.

Syntax:
ALTER TABLE table_name DROP COLUMN
column_name;

Example:
ALTER TABLE Employee DROP COLUMN EmpSalary;
ALTER TABLE - Modify Column
Definition:
Changes the data type or size of a column.

Syntax:
ALTER TABLE table_name MODIFY column_name
new_datatype;

Example:
ALTER TABLE Employee MODIFY EmpSalary
DECIMAL(10,2);

You might also like