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

MySQL Database

MySQL is an open-source relational database management system that utilizes SQL for data management and is widely used in web applications. It supports various operations through DDL, DML, DQL, and DCL, allowing users to create, manipulate, query, and manage access to data. The document also explains constraints in MySQL, such as UNIQUE, NOT NULL, PRIMARY KEY, Composite Key, and FOREIGN KEY, which ensure data integrity within the database.

Uploaded by

kalkipatel85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views13 pages

MySQL Database

MySQL is an open-source relational database management system that utilizes SQL for data management and is widely used in web applications. It supports various operations through DDL, DML, DQL, and DCL, allowing users to create, manipulate, query, and manage access to data. The document also explains constraints in MySQL, such as UNIQUE, NOT NULL, PRIMARY KEY, Composite Key, and FOREIGN KEY, which ensure data integrity within the database.

Uploaded by

kalkipatel85
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

MySQL Database

MySQL is an open-source relational database management system (RDBMS) that uses


Structured Query Language (SQL) to manage, manipulate, and retrieve data stored in
databases. It is widely used for web applications and is part of the popular LAMP stack
(Linux, Apache, MySQL, PHP/Perl/Python). MySQL is known for its ease of use,
scalability, and performance.

MySQL is developed, distributed, and supported by Oracle Corporation. It is


available in various editions, such as the community edition (free and open-source) and
the enterprise edition (with additional features and support). MySQL provides a flexible
and efficient way to store, organize, and manage data, making it suitable for a wide
range of applications, from small personal projects to large-scale enterprise systems.

Introduction to SQL
SQL (Structured Query Language) is a standard language used to communicate with and
manipulate relational databases such as MySQL, PostgreSQL, Oracle, and Microsoft SQL
Server. With SQL, you can create, modify, and query data in databases, manage access
control, and more.

Creating a Database
To create a new database, use the CREATE DATABASE statement followed by the
database name.

Example:
CREATE DATABASE FrontlinesMediaDB;
This command creates a new database called 'FrontlinesMediaDB'. To work with this
database, you need to select it using the USE statement:

USE FrontlinesMediaDB;

DDL (Data Definition Language)


DDL, or Data Definition Language, is a subset of SQL used to create, modify, and delete
the structure of database objects such as tables, schemas, and views. DDL does not deal
with data manipulation; it focuses on the structure of the database. In this section, we
will explain DDL in detail with examples in MySQL.

1
1. CREATE TABLE
The CREATE TABLE statement is used to create a new table with columns, their data
types, and constraints.

Example:
CREATE TABLE FLM_Articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255),
published_date DATE,
content TEXT
);

This command creates a table called 'FLM_Articles' with five columns: 'id', 'title',
'author', 'published_date', and 'content'. The 'id' column is set to AUTO_INCREMENT and
is defined as the PRIMARY KEY, ensuring that it has a unique value for each row.

2. ALTER TABLE
The ALTER TABLE statement is used to modify an existing table by adding, modifying, or
deleting columns, as well as adding or dropping constraints.

Example - Add Column:


ALTER TABLE FLM_Articles
ADD COLUMN category VARCHAR(255);

This command adds a new column called 'category' with a VARCHAR(255) data type to
the existing 'FLM_Articles' table.

Example - Modify Column:


ALTER TABLE FLM_Articles
MODIFY COLUMN author VARCHAR(100);

This command modifies the 'author' column in the 'FLM_Articles' table to have a
VARCHAR(100) data type.

Example - Drop Column:


ALTER TABLE FLM_Articles
DROP COLUMN content;

This command removes the 'content' column from the 'FLM_Articles' table.

2
3. RENAME:
The RENAME command is used to rename one or more tables. This operation is atomic,
meaning it's performed in a single step and cannot be interrupted.

Example:
-- Create a table for FLM_Articles
CREATE TABLE FLM_Articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
published_date DATE NOT NULL
);

-- Rename the FLM_Articles table to FLM_BlogPosts


RENAME TABLE FLM_Articles TO FLM_BlogPosts;

In this example, the FLM_Articles table is created, and the RENAME command is used to
rename it to FLM_BlogPosts.

These examples demonstrate how to use the TRUNCATE and RENAME commands in
MySQL to delete all data from a table and rename a table, respectively.

4. TRUNCATE:
The TRUNCATE command is used to delete all data from a table without deleting the
table structure itself. It is more efficient than the DELETE command when removing all
data because it does not generate any undo logs or write any data to the binary log.

Example:
-- Create a table for FLM_Articles
CREATE TABLE FLM_Articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
published_date DATE NOT NULL
);

-- Insert data into the FLM_Articles table


INSERT INTO FLM_Articles (title, author, content, published_date)

3
VALUES ('MySQL Tutorial', 'John Doe', 'This is a MySQL tutorial...', '2023-05-02');

-- Truncate the FLM_Articles table, deleting all data


TRUNCATE TABLE FLM_Articles;

In this example, the FLM_Articles table is created and populated with data. Then, the
TRUNCATE command is used to delete all data from the table.

5. DROP TABLE
The DROP TABLE statement is used to delete an existing table and all its data.

Example:
DROP TABLE FLM_Articles;

This command deletes the 'FLM_Articles' table from the database.

6. CREATE INDEX
The CREATE INDEX statement is used to create an index on one or more columns of a
table. Indexes can help speed up data retrieval operations.

Example:
CREATE INDEX idx_FLM_Articles_published_date ON FLM_Articles(published_date);

This command creates an index named 'idx_FLM_Articles_published_date' on the


'published_date' column of the 'FLM_Articles' table.

7. DROP INDEX
The DROP INDEX statement is used to delete an existing index.

Example:
DROP INDEX idx_FLM_Articles_published_date ON FLM_Articles;

This command deletes the 'idx_FLM_Articles_published_date' index from the


'FLM_Articles' table.
These examples provide a detailed understanding of DDL in MySQL, including creating
tables, altering tables, dropping tables, creating indexes, and dropping indexes.

4
DML (Data Manipulation Language)
DML, or Data Manipulation Language, is a subset of SQL used to retrieve, insert, update,
and delete data within database tables. It focuses on manipulating the data stored in a
database rather than the structure of the database itself. In this section, we will explain
DML in detail with examples in MySQL.

1. SELECT
The SELECT statement is used to retrieve data from one or more tables. You can specify
the columns you want to retrieve, apply filters using the WHERE clause, and sort or
group the results.

Example:
SELECT title, author, published_date FROM FLM_Articles WHERE category =
'Technology';

This command retrieves the 'title', 'author', and 'published_date' columns for all rows in
the 'FLM_Articles' table where the 'category' is 'Technology'.

2. INSERT INTO
The INSERT INTO statement is used to insert new data into a table. You can specify the
columns you want to insert data into and provide the data values.

Example:
INSERT INTO FLM_Articles (title, author, published_date, category)
VALUES ('New FLM Tech Gadget', 'John Smith', '2023-05-05', 'Technology');

This command inserts a new row into the 'FLM_Articles' table with the values 'New FLM
Tech Gadget' for 'title', 'John Smith' for 'author', '2023-05-05' for 'published_date', and
'Technology' for 'category'.

3. UPDATE
The UPDATE statement is used to modify existing data in a table. You can specify the new
values for the columns and apply filters using the WHERE clause to determine which
rows should be updated.
Example:
UPDATE FLM_Articles SET author = 'Jane Doe' WHERE id = 1;

This command updates the 'author' column to 'Jane Doe' for the row in the
'FLM_Articles' table where the 'id' is 1.

5
4. DELETE
The DELETE statement is used to remove data from a table. You can apply filters using
the WHERE clause to determine which rows should be deleted.
Example:
DELETE FROM FLM_Articles WHERE id = 1;

This command deletes the row in the 'FLM_Articles' table where the 'id' is 1.
These examples provide a detailed understanding of DML in MySQL, including selecting,
inserting, updating, and deleting data in tables.

DQL (Data Query Language)


DQL, or Data Query Language, is a subset of SQL used to retrieve data from databases,
particularly from tables. It focuses on querying data rather than manipulating the data or
the structure of the database itself. In this section, we will explain DQL in detail with
examples in MySQL.
The primary DQL statement is the SELECT statement, which can be used with various
clauses and functions to fetch data according to specific conditions, sorting, and
grouping.

1. Basic SELECT
The simplest form of a SELECT statement is used to retrieve all columns from a table.
Example:
SELECT * FROM FLM_Articles;

This command retrieves all columns and rows from the 'FLM_Articles' table.

2. SELECT with specific columns


To retrieve only specific columns from a table, specify the column names after the
SELECT keyword.
Example:
SELECT title, author, published_date FROM FLM_Articles;

This command retrieves the 'title', 'author', and 'published_date' columns for all rows in
the 'FLM_Articles' table.

3. SELECT with WHERE clause


The WHERE clause is used to filter the results based on one or more conditions.

6
Example:
SELECT title, author, published_date FROM FLM_Articles WHERE category =
'Technology';

This command retrieves the 'title', 'author', and 'published_date' columns for all rows in
the 'FLM_Articles' table where the 'category' is 'Technology'.

4. SELECT with ORDER BY clause


The ORDER BY clause is used to sort the results based on one or more columns, in either
ascending (ASC) or descending (DESC) order.
Example:
SELECT title, author, published_date FROM FLM_Articles ORDER BY published_date
DESC;

This command retrieves the 'title', 'author', and 'published_date' columns for all rows in
the 'FLM_Articles' table and sorts the results by the 'published_date' column in
descending order.

5. SELECT with GROUP BY and aggregate functions


The GROUP BY clause is used to group rows with the same values in specified columns. It
is often used with aggregate functions like COUNT, SUM, AVG, MIN, or MAX to perform
calculations on each group.
Example:
SELECT category, COUNT(*) as total_articles FROM FLM_Articles GROUP BY category;

This command retrieves the number of articles in each category in the 'FLM_Articles'
table by grouping the rows by the 'category' column and counting the number of rows in
each group.
These examples provide a detailed understanding of DQL in MySQL, including selecting
data, applying filters with the WHERE clause, sorting results with the ORDER BY clause,
and grouping data with the GROUP BY clause.

DCL (Data Control Language)


DCL, or Data Control Language, is a subset of SQL used to manage access control and
permissions for database objects, such as tables, views, and procedures. It focuses on
the security and authorization aspects of a database rather than data manipulation or
the structure of the database itself. In this section, we will explain DCL in detail with
examples in MySQL.

7
The primary DCL statements are GRANT and REVOKE, which are used to provide and
remove access privileges to database objects.

1. GRANT
The GRANT statement is used to give specific privileges to a user or a group of users on a
database object.
Example:
GRANT SELECT, INSERT, UPDATE ON FLM_Articles TO 'flm_user'@'localhost';

This command grants the 'flm_user' user the SELECT, INSERT, and UPDATE privileges on
the 'FLM_Articles' table.

2. REVOKE
The REVOKE statement is used to remove specific privileges from a user or a group of
users on a database object.
Example:
REVOKE INSERT, UPDATE ON FLM_Articles FROM 'flm_user'@'localhost';

This command revokes the INSERT and UPDATE privileges from the 'flm_user' user on
the 'FLM_Articles' table.
It's important to note that the user management and privilege system in MySQL is quite
extensive, and the examples provided here are a basic introduction to managing access
control using DCL. In real-world scenarios, you may need to create and manage multiple
users with different privileges, as well as manage access to multiple database objects.
These examples provide a detailed understanding of DCL in MySQL, including granting
and revoking access privileges on database objects.

Example code for DDL, DML, DQL, and DCL:


-- DDL: Create a table for articles
CREATE TABLE FLM_Articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
published_date DATE NOT NULL
);

-- DML: Insert data into the articles table

8
INSERT INTO FLM_Articles (title, author, content, published_date)
VALUES ('MySQL Tutorial', 'John Doe', 'This is a MySQL tutorial...', '2023-05-02');

-- DQL: Select data from the articles table


SELECT * FROM FLM_Articles WHERE author = 'John Doe';

-- DCL: Grant permissions to a user on the articles table


GRANT SELECT, INSERT, UPDATE ON FLM_Articles TO 'flm_user'@'localhost';

In this example:
1. A DDL statement is used to create the FLM_Articles table, defining its structure
with various columns and constraints.
2. A DML statement is used to insert a new row into the FLM_Articles table,
providing values for the title, author, content, and published_date columns.
3. A DQL statement is used to query the FLM_Articles table, selecting all rows where
the author is 'John Doe'.
4. A DCL statement is used to grant the SELECT, INSERT, and UPDATE permissions to
a user called 'flm_user' on the FLM_Articles table.
This example demonstrates how to use DDL, DML, DQL, and DCL statements in MySQL to
create, manipulate, query, and manage access control for a database table.

Constraints in MySQL
Constraints are rules applied to columns within a table to ensure data integrity and
maintain the relationships between tables in a database. In this section, we will explain
various constraints in MySQL, including Unique Key, Not Null, Primary Key, Composite
Key, and Foreign Key.

1. UNIQUE Key
A UNIQUE constraint ensures that all values in a column are unique, meaning no two
rows can have the same value in the specified column.
Example:
CREATE TABLE FLM_Authors (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL
);

9
In this example, the 'email' column has a UNIQUE constraint, ensuring that each author
has a unique email address.

2. NOT NULL
A NOT NULL constraint ensures that a column cannot have a NULL value, meaning a
value must be provided for the specified column when a new row is inserted.
Example:
CREATE TABLE FLM_Articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author_id INT NOT NULL,
content TEXT NOT NULL
);

In this example, the 'title', 'author_id', and 'content' columns have NOT NULL
constraints, ensuring that these columns have values for every row in the table.

3. PRIMARY Key
A PRIMARY KEY constraint uniquely identifies each row in a table. It consists of one or
more columns, and it enforces the uniqueness and NOT NULL constraints for the
specified columns.
Example:
CREATE TABLE FLM_Articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author_id INT NOT NULL,
content TEXT NOT NULL
);

In this example, the 'id' column is defined as the PRIMARY KEY, ensuring that it has a
unique value for each row in the table.

4. Composite Key
A Composite Key is a key that consists of two or more columns to uniquely identify each
row in a table. It is used when a single column is not sufficient to guarantee uniqueness.
Example:
CREATE TABLE FLM_Article_Tag_Relations (
article_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (article_id, tag_id)

10
);

In this example, the combination of 'article_id' and 'tag_id' columns forms a Composite
Key, ensuring that each row has a unique combination of both columns.

5. FOREIGN Key
A FOREIGN KEY constraint is used to create a relationship between two tables. It
enforces referential integrity by ensuring that the values in a column of one table match
the values in the primary key column of another table.
Example:
CREATE TABLE FLM_Articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
author_id INT NOT NULL,
content TEXT NOT NULL,
FOREIGN KEY (author_id) REFERENCES FLM_Authors(id)
);

In this example, the 'author_id' column in the 'FLM_Articles' table is defined as a


FOREIGN KEY that references the 'id' column in the 'FLM_Authors' table. This ensures
that every 'author_id' value in the 'FLM_Articles' table corresponds to a valid 'id' value in
the 'FLM_Authors' table.
These examples provide a detailed understanding of constraints in MySQL, including
Unique Key, Not Null, Primary Key, Composite Key, and Foreign Key.

Example code for Constraints:


In this example, we will create a simple EdTech database schema to manage courses,
lessons, and students using MySQL. We will incorporate various constraints, including
Unique Key, Not Null, Primary Key, Composite Key, and Foreign Key.
-- Create a table for students
CREATE TABLE FLM_Students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL
);

-- Create a table for courses


CREATE TABLE FLM_Courses (

11
course_id INT AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(255) UNIQUE NOT NULL,
course_description TEXT NOT NULL
);

-- Create a table for lessons


CREATE TABLE FLM_Lessons (
lesson_id INT AUTO_INCREMENT PRIMARY KEY,
course_id INT NOT NULL,
lesson_name VARCHAR(255) NOT NULL,
lesson_content TEXT NOT NULL,
FOREIGN KEY (course_id) REFERENCES FLM_Courses(course_id)
);

-- Create a table for student_course_relations


CREATE TABLE FLM_Student_Course_Relations (
student_id INT NOT NULL,
course_id INT NOT NULL,
enrollment_date DATE NOT NULL,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES FLM_Students(student_id),
FOREIGN KEY (course_id) REFERENCES FLM_Courses(course_id)
);

In this example:
1. The FLM_Students table stores student information, with a PRIMARY KEY
constraint on the student_id column and a UNIQUE constraint on the email
column.
2. The FLM_Courses table stores course information, with a PRIMARY KEY constraint
on the course_id column and a UNIQUE constraint on the course_name column.
3. The FLM_Lessons table stores lesson information, with a PRIMARY KEY constraint
on the lesson_id column and a FOREIGN KEY constraint on the course_id column,
referencing the course_id column in the FLM_Courses table.
4. The FLM_Student_Course_Relations table stores the relationships between
students and courses, with a Composite PRIMARY KEY consisting of the
student_id and course_id columns, and FOREIGN KEY constraints on both the
student_id and course_id columns, referencing the student_id column in the
FLM_Students table and the course_id column in the FLM_Courses table,
respectively.

12
This EdTech example demonstrates how to create a MySQL database schema using
various constraints.

13

You might also like