0% found this document useful (0 votes)
1 views3 pages

mysql_sql_guide (1)

This document provides an overview of SQL and MySQL syntax, detailing the different types of commands including DDL, DML, DCL, and TCL. It includes examples of creating tables, integrity constraints, data manipulation, and various SQL operations such as joins, views, and triggers. The guide serves as a comprehensive reference for interacting with relational databases using SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

mysql_sql_guide (1)

This document provides an overview of SQL and MySQL syntax, detailing the different types of commands including DDL, DML, DCL, and TCL. It includes examples of creating tables, integrity constraints, data manipulation, and various SQL operations such as joins, views, and triggers. The guide serves as a comprehensive reference for interacting with relational databases using SQL.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

===============================

SQL Overview & MySQL Syntax Guide


===============================

1. Overview of SQL
--------------------
SQL (Structured Query Language) is used to interact with relational databases.
- DDL: Data Definition Language (CREATE, ALTER, DROP)
- DML: Data Manipulation Language (INSERT, UPDATE, DELETE, SELECT)
- DCL: Data Control Language (GRANT, REVOKE)
- TCL: Transaction Control Language (COMMIT, ROLLBACK)

2. Data Definition Commands (DDL)


---------------------------------
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
dept VARCHAR(10)
);

ALTER TABLE students ADD COLUMN email VARCHAR(100);

DROP TABLE students;

3. Integrity Constraints
------------------------
-- Primary Key Constraint
CREATE TABLE users (
user_id INT PRIMARY KEY,
username VARCHAR(50)
);

-- Unique Constraint
CREATE TABLE accounts (
acc_id INT,
email VARCHAR(100) UNIQUE
);

-- Domain Constraint
CREATE TABLE members (
member_id INT,
age INT CHECK (age > 17)
);

-- Referential Integrity
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);

CREATE TABLE employee (


emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);

-- Check Constraint
CREATE TABLE workers (
id INT,
age INT CHECK (age >= 18 AND age <= 60)
);

4. Data Manipulation Commands (DML)


-----------------------------------
INSERT INTO students (id, name, age, dept) VALUES (1, 'Alice', 20, 'CS');

UPDATE students SET age = 21 WHERE id = 1;

DELETE FROM students WHERE id = 1;

SELECT * FROM students;

5. Data Control Commands (DCL)


------------------------------
GRANT SELECT, INSERT ON college.* TO 'user1'@'localhost';

REVOKE INSERT ON college.* FROM 'user1'@'localhost';

6. Set and String Operations


----------------------------
-- Set Operation
SELECT name FROM students
UNION
SELECT name FROM alumni;

-- String Functions
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;
SELECT UPPER(name) FROM students;
SELECT LOWER(name) FROM students;
SELECT SUBSTRING(name, 1, 3) FROM students;

7. Aggregate Functions, GROUP BY, HAVING


----------------------------------------
SELECT COUNT(*), AVG(age), MAX(age), MIN(age), SUM(age) FROM students;

SELECT dept, COUNT(*) FROM students GROUP BY dept;

SELECT dept, COUNT(*) AS total FROM students GROUP BY dept HAVING total > 5;

8. Views
--------
CREATE VIEW cs_students AS
SELECT name, age FROM students WHERE dept = 'CS';

SELECT * FROM cs_students;

9. Joins
--------
-- Inner Join
SELECT s.name, d.dept_name
FROM students s
JOIN department d ON s.dept = d.dept_id;

-- Left Join
SELECT s.name, d.dept_name
FROM students s
LEFT JOIN department d ON s.dept = d.dept_id;

-- Right Join
SELECT s.name, d.dept_name
FROM students s
RIGHT JOIN department d ON s.dept = d.dept_id;

10. Nested and Complex Queries


------------------------------
-- Subquery
SELECT name FROM students WHERE age = (SELECT MAX(age) FROM students);

-- EXISTS
SELECT name FROM students s
WHERE EXISTS (SELECT * FROM department d WHERE s.dept = d.dept_id);

11. Triggers
------------
DELIMITER //
CREATE TRIGGER before_insert_student
BEFORE INSERT ON students
FOR EACH ROW
BEGIN
SET NEW.name = UPPER(NEW.name);
END;
//
DELIMITER ;

You might also like