MySQL Complete Guide
1. CREATE DATABASE
Definition:
Creates a new database in MySQL.
Syntax:
CREATE DATABASE dbname;
Example:
CREATE DATABASE School;
Expected Output:
Query OK, 1 row affected
2. CREATE TABLE
Definition:
Creates a new table inside a database.
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
Example:
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT
);
MySQL Complete Guide
Expected Output:
Query OK, 0 rows affected
3. INSERT ROW
Definition:
Adds a single row of data to a table.
Syntax:
INSERT INTO table_name (col1, col2) VALUES (val1, val2);
Example:
INSERT INTO Students (ID, Name, Age) VALUES (1, 'Ali', 18);
Expected Output:
Query OK, 1 row affected
4. INSERT MULTIPLE ROWS
Definition:
Inserts several rows in a single query.
Syntax:
INSERT INTO table_name (col1, col2) VALUES
(val1a, val2a),
(val1b, val2b);
Example:
INSERT INTO Students (ID, Name, Age) VALUES
(2, 'Sara', 20),
(3, 'John', 17);
Expected Output:
MySQL Complete Guide
Query OK, 2 rows affected
5. CONSTRAINTS
Definition:
Used to enforce rules on data like uniqueness, no nulls, referential integrity.
Syntax:
PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK, DEFAULT
Example:
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(50) NOT NULL,
CreditHours INT CHECK (CreditHours > 0)
);
Expected Output:
Query OK, 0 rows affected
6. SELECT with WHERE
Definition:
Retrieves rows that match a condition.
Syntax:
SELECT * FROM table_name WHERE condition;
Example:
SELECT * FROM Students WHERE Age > 18;
Expected Output:
+----+-------+-----+
MySQL Complete Guide
| ID | Name | Age |
+----+-------+-----+
| 2 | Sara | 20 |
+----+-------+-----+