Untitled

Anonymous
MySQL
10/28/2025 5:09 PM
4.4 KB
19
Indexable
Never
CREATE TABLE Department (
    dept_id INT PRIMARY KEY,
    dept_name VARCHAR(50) UNIQUE
);

CREATE TABLE Employee (
    emp_id INT PRIMARY KEY,
    emp_name VARCHAR(50) NOT NULL,
    salary DECIMAL(10,2) CHECK (salary > 0),
    dept_id INT,
    FOREIGN KEY (dept_id)
        REFERENCES Department(dept_id)
        ON DELETE SET NULL
        ON UPDATE CASCADE
);


ALTER TABLE table_name
ADD column_name datatype;

ALTER TABLE table_name
MODIFY column_name new_datatype;

ALTER TABLE table_name
DROP COLUMN column_name;

ALTER TABLE table_name
ADD CONSTRAINT constraint_name UNIQUE (col);


DROP TABLE Department CASCADE CONSTRAINTS;
✅ CASCADE CONSTRAINTS → drops dependent FK constraints.


INSERT INTO table_name VALUES (...);

UPDATE table_name
SET column = value, ...
WHERE condition;
⚠️ If WHERE is omitted → updates all rows.

DELETE FROM table_name
WHERE condition;


SELECT column_list
FROM table_name
WHERE condition
GROUP BY column_list
HAVING condition
ORDER BY column [ASC|DESC];

Operators:
=, !=, >, <, >=, <=, AND, OR, NOT

WHERE salary BETWEEN 30000 AND 50000;
✅ Includes both 30000 and 50000.

WHERE dept_id IN (10, 20, 30);
WHERE dept_id NOT IN (10, 20, 30);

| Pattern | Meaning                |
| ------- | ---------------------- |
| `'A%'`  | Starts with A          |
| `'%A'`  | Ends with A            |
| `'%A%'` | Contains A             |
| `'A_'`  | A followed by one char |

WHERE emp_name LIKE 'S%';

ORDER BY salary DESC;

| Function  | Description |
| --------- | ----------- |
| `COUNT()` | Counts rows |
| `SUM()`   | Total       |
| `AVG()`   | Average     |
| `MIN()`   | Minimum     |
| `MAX()`   | Maximum     |

➤ GROUP BY
Used to group rows for aggregates.
⚠️ All non-aggregated columns in SELECT must appear in GROUP BY.

SELECT dept_id, AVG(salary)
FROM employee
GROUP BY dept_id;

➤ HAVING
Filters groups (used after GROUP BY).
SELECT dept_id, AVG(salary)
FROM employee
GROUP BY dept_id
HAVING AVG(salary) > 50000;
--> Aggregate function can be used in having but not in where


➤ ALL
Condition must be true for all values.
SELECT * FROM employee
WHERE salary > ALL (SELECT salary FROM employee WHERE dept_id = 10);
--> Employees earning more than every salary in dept 10.

➤ ANY / SOME
True if condition holds for any one value.
SELECT * FROM employee
WHERE salary > ANY (SELECT salary FROM employee WHERE dept_id = 10);
--> Employees earning more than at least one salary in dept 10.
(ANY and SOME mean the same.)

| Type                | Description                                    | Example                                                                                            |
| ------------------- | ---------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| **INNER JOIN**      | Returns matching rows only                     | `SELECT e.emp_name, d.dept_name FROM employee e INNER JOIN department d ON e.dept_id = d.dept_id;` |
| **LEFT JOIN**       | All rows from left + matching from right       | `LEFT JOIN department d`                                                                           |
| **RIGHT JOIN**      | All rows from right + matching from left       | `RIGHT JOIN department d`                                                                          |
| **FULL OUTER JOIN** | All rows from both sides (null where no match) |                                                                                                    |
| **CROSS JOIN**      | Cartesian product                              |                                                                                                    |

CREATE VIEW high_salary AS
SELECT emp_name, salary
FROM employee
WHERE salary > 50000;
SELECT * FROM high_salary;
DROP VIEW high_salary;


CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE}
ON table_name
FOR EACH ROW
BEGIN
    -- Trigger logic / code
    -- You can use :NEW and :OLD (in Oracle/MySQL)
    -- or NEW and OLD (in standard SQL)
END;


DELIMITER //

CREATE TRIGGER trg_check_discount
BEFORE INSERT OR UPDATE ON orders
FOR EACH ROW
BEGIN
    IF NEW.discount > (0.2 * NEW.total_price) THEN
        SIGNAL SQLSTATE '45000'
        SET MESSAGE_TEXT = 'Error: Discount cannot exceed 20% of total price';
    END IF;
END;
//

DELIMITER ;
Editor is loading...
Leave a Comment