MySQL Constraints Commands
Dr Gowtham Ramesh
Ensures that a column cannot have a
NULL value.
CREATE TABLE table_name (
column_name datatype NOT NULL
);
Ensures that all values in a column are
different.
CREATE TABLE table_name (
column_name datatype UNIQUE
);
A combination of a NOT NULL and
UNIQUE. Uniquely identifies each row
in a table.
CREATE TABLE table_name (
column_name datatype PRIMARY
KEY
);
Ensures the referential integrity of the
data in one table to match values in
another table.
CREATE TABLE table_name (
column_name datatype,
FOREIGN KEY (column_name)
REFERENCES
another_table(another_column)
);
Ensures that the value in a column
meets a specific condition.
CREATE TABLE table_name (
column_name datatype,
CHECK (condition)
);
Sets a default value for a column if no
value is specified.
CREATE TABLE table_name (
column_name datatype DEFAULT
default_value
);
MODIFY CONSTRAINT
ALTER TABLE table_name
MODIFY column_name datatype NOT
NULL;
ALTER TABLE table_name
ADD UNIQUE (column_name);
MODIFY CONSTRAINT
ALTER TABLE table_name
ADD PRIMARY KEY (column_name);
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column_name)
REFERENCES
referenced_table(referenced_column);
ALTER TABLE table_name
ADD CHECK (condition);
ALTER TABLE table_name
ALTER COLUMN column_name SET DEFAULT
default_value;
MySQL Join Operations
Commands
Selects records that have matching values in
both tables.
SELECT columns
FROM table1
INNER JOIN table2 ON
table1.common_column =
table2.common_column;
Selects records that have matching values in
both tables
Example:
SELECT employees.name,
departments.department_name
FROM employees
INNER JOIN departments ON
employees.department_id =
departments.department_id;
Selects all records from the left table,
and the matched records from the
right table. The result is NULL from the
right side if there is no match
SELECT columns
FROM table1
LEFT JOIN table2 ON
table1.common_column =
table2.common_column;
Selects all records from the left table, and the
matched records from the right table. The
result is NULL from the right side if there is no
match
Example:
SELECT employees.name,
departments.department_name
FROM employees
LEFT JOIN departments ON
employees.department_id =
departments.department_id;
Selects all records from the right table,
and the matched records from the left
table. The result is NULL from the left
side if there is no match
SELECT columns
FROM table1
RIGHT JOIN table2 ON
table1.common_column =
table2.common_column;
Selects all records from the right table, and
the matched records from the left table. The
result is NULL from the left side if there is no
match
Example:
SELECT employees.name,
departments.department_name
FROM employees
RIGHT JOIN departments ON
employees.department_id =
departments.department_id;
Selects all records when there is a match in either left or right table.
Note that MySQL does not support FULL OUTER JOIN natively, but you
can achieve this using a UNION of LEFT JOIN and RIGHT JOIN
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.common_column = table2.common_column
UNION
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.common_column =
table2.common_column;
Selects all records when there is a match in either left or right
table. Note that MySQL does not support FULL OUTER JOIN
natively, but you can achieve this using a UNION of LEFT JOIN
and RIGHT JOIN
Example:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id =
departments.department_id
UNION
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id =
departments.department_id;
Returns the Cartesian product of the two tables
SELECT columns
FROM table1
CROSS JOIN table2;
Example:
SELECT employees.name, departments.department_name
FROM employees
CROSS JOIN departments;
A self join is a regular join but the
table is joined with itself
SELECT a.columns, b.columns
FROM table_name a, table_name b
WHERE a.common_column =
b.common_column;
A self join is a regular join but the
table is joined with itself.
Example:
SELECT a.name AS Employee1, b.name
AS Employee2
FROM employees a, employees b
WHERE a.manager_id =
b.employee_id;
MySQL Set Operations
Commands
Combines the results of two or more SELECT
queries and removes duplicate rows
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Combines the results of two or more
SELECT queries and includes all
duplicate rows
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Finds the common rows between two SELECT
queries. MySQL does not support INTERSECT
natively, but you can achieve this using an INNER
JOIN or subqueries
Using INNER JOIN:
SELECT column_name(s) FROM table1
INNER JOIN table2 ON table1.column_name =
table2.column_name;
Using Subqueries:
SELECT column_name(s) FROM table1
WHERE column_name IN (SELECT column_name
FROM table2);
Finds the rows that are in the first SELECT
query but not in the second. MySQL does not
support MINUS natively, but you can achieve
this using a LEFT JOIN or subqueries.
Using LEFT JOIN:
SELECT column_name(s) FROM table1
LEFT JOIN table2 ON table1.column_name =
table2.column_name
WHERE table2.column_name IS NULL;
MySQL View Operations
Commands
Creates a view based on a SELECT query
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
CREATE VIEW employee_view AS
SELECT name, department_id
FROM employees
WHERE status = 'active';
Selects data from a view just like a table
SELECT column1, column2, ...
FROM view_name;
Example:
SELECT name, department_id
FROM employee_view;
Updates the data in the underlying table(s) through the view.
UPDATE view_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE employee_view
SET department_id = 2
WHERE name = 'John Doe';
Inserts data into the underlying table(s) through the view
INSERT INTO view_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
INSERT INTO employee_view (name, department_id)
VALUES ('Jane Doe', 3);
Deletes data from the underlying table(s) through the view
DELETE FROM view_name
WHERE condition;
Example:
DELETE FROM employee_view
WHERE name = 'John Doe';
Changes the definition of an existing view
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
CREATE OR REPLACE VIEW employee_view AS
SELECT name, department_id
FROM employees
WHERE status = 'active' AND department_id IS NOT NULL;
Removes a view from the database
DROP VIEW view_name;
Example:
DROP VIEW employee_view;