DATABASE – MySQL
Database and RDBMS Basics ⭐⭐
• Database: Organised collection of structured information stored electronically in a computer
system.
• DBMS: Software used to create, manage and control databases.
• RDBMS: Data stored in tables with relations using Primary Key and Foreign Key.
• MySQL: Popular open-source RDBMS.
MySQL and Its Importance ⭐⭐⭐
• Open-source software, free to use.
• Powerful and supports large databases.
• Works on multiple operating systems.
• Supports many languages like PHP, Java, C, C++.
• Fast processing and high performance.
• Customizable as per user needs.
Types of SQL Commands ⭐⭐⭐
Data Definition Language DDL ⭐⭐
Used to define or change structure of database objects. Auto-committed. - CREATE - ALTER - DROP -
TRUNCATE
Data Manipulation Language DML ⭐⭐⭐
Used to modify data in tables. Not auto-committed. - INSERT - UPDATE - DELETE
Data Control Language DCL ⭐⭐
Used to grant or revoke permissions. - GRANT - REVOKE
Transaction Control Language TCL ⭐
Used with DML commands. - COMMIT - ROLLBACK - SAVEPOINT
Data Query Language DQL ⭐⭐
Used to fetch data. - SELECT - SHOW - HELP
1
Creating, Selecting and Removing Database ⭐⭐
• Create Database CREATE DATABASE database_name;
• Select Database USE database_name;
• Remove Database DROP DATABASE database_name;
MySQL Data Types ⭐⭐⭐
• CHAR: Fixed length string
• VARCHAR: Variable length string
• TEXT: Large text
• INT: Integer values
• FLOAT / DOUBLE: Decimal values
• DATE: YYYY-MM-DD
• DATETIME: YYYY-MM-DD HH:MM:SS ⭐⭐⭐
• TIME: HH:MM:SS
• YEAR: Year values
Data type depends on - Storage size - Type of value stored
MySQL Tables ⭐⭐
• Table consists of rows and columns.
• Rows represent records.
• Columns represent attributes.
• Degree: Number of columns.
• Cardinality: Number of rows.
Rules for Naming a Table ⭐⭐⭐
• Maximum 30 characters.
• Must start with an alphabet.
• Can contain alphabets, numbers and underscore.
• No reserved words allowed.
Creating a Table ⭐⭐⭐
• CREATE TABLE command is used.
• Each column has name, data type and size.
• PRIMARY KEY uniquely identifies records.
• NOT NULL does not allow empty values.
2
Constraints ⭐⭐⭐
Constraints ensure validity of data. - PRIMARY KEY ⭐⭐⭐ - NOT NULL ⭐⭐⭐ - UNIQUE - FOREIGN KEY
Displaying Table Structure ⭐⭐⭐
• DESCRIBE table_name;
• DESC table_name;
Inserting Data in Table ⭐⭐
• INSERT INTO table_name VALUES(...);
• Multiple rows can be inserted in a single query.
ALTER TABLE Command ⭐⭐⭐
Used to modify structure of a table. - Add columns - Modify columns - Rename column - Drop column - Add
or delete constraints
SELECT Statement ⭐⭐⭐
Used to retrieve data from table.
• Select all columns SELECT * FROM table_name;
• Select specific columns SELECT column1, column2 FROM table_name;
• Using WHERE clause ⭐⭐⭐ Used to retrieve selected rows based on condition.
DISTINCT and ALL ⭐⭐
• DISTINCT removes duplicate values.
• ALL displays all values.
Pattern Matching using LIKE ⭐⭐⭐
• % matches any number of characters.
• _ matches single character.
3
BETWEEN Clause ⭐⭐⭐
• Used to search values within a range.
• Includes both lower and upper limits.
Operators in MySQL ⭐⭐⭐
Arithmetic Operators ⭐⭐
Used in calculations. - + - * / %
Relational Operators ⭐⭐
• = != < > <= >=
Logical Operators ⭐⭐⭐
• AND
• OR
• NOT
Sorting Data ⭐⭐
• ORDER BY clause used.
• ASC for ascending
• DESC for descending
Deleting Data ⭐⭐⭐
• DELETE FROM table_name;
• DELETE FROM table_name WHERE condition;
Updating Data ⭐⭐⭐
• UPDATE table_name SET column=value;
• UPDATE table_name SET column=value WHERE condition;
Aggregate Functions ⭐⭐⭐
Used to perform calculations on a group of values. - SUM() - AVG() - MAX() - MIN() - COUNT() - COUNT(*)
4
Very Important One Mark Facts ⭐⭐⭐
• MySQL statement ends with semicolon
• Command to delete database physically: DROP DATABASE
• Command to view table structure: DESCRIBE / DESC
• DML modifies data
• Constraints ensure data validity