Module 7 Basic SQL Statements
Module 7 Basic SQL Statements
DATABASE
Objective:
1. What is SQL?
Definition: SQL (Structured Query Language) is the standard language used for managing
and manipulating relational databases.
2. Basic Concepts:
a. Database
i. A collection of data organized in tables.
ii. Tables contain rows (records) and columns (fields).
b. Tables
i. The basic unit of a database where data is stored.
ii. Tables are made up of rows (records) and columns (fields).
c. SQL Statements
i. SQL commands that are used to interact with the database. These include:
1. Data Definition Language (DDL): Defines the structure of the
database (e.g., CREATE, ALTER, DROP).
2. Data Manipulation Language (DML): Manages data within tables
(e.g., SELECT, INSERT, UPDATE, DELETE)
ii. Difference between DDL and DML
1. DDL (Data Definition Language)
a. DDL is used to define, modify, and remove database
structures (such as tables, indexes, and schemas). It deals
with the structure of the database rather than the data
within it.
b. Common DDL Commands:
i. CREATE: Used to create database objects like tables,
views, and indexes.
ii. ALTER: Used to modify an existing database object,
such as adding or dropping columns from a table.
iii. DROP: Used to delete database objects, such as a
table or a view.
iv. TRUNCATE: Used to remove all rows from a table,
but the table structure remains.
c. Examples:
i. CREATE TABLE: Creates a new table.
ii. ALTER TABLE: Modifies an existing table's
structure.
iii. DROP TABLE: Deletes a table from the database.
d. Key Points:
i. DDL changes the structure of the database.
ii. DDL operations are typically permanent and
irreversible (e.g., dropping a table).
iii. DDL commands don’t operate on data itself, but
rather on the schema.
2. DML (Data Manipulation Language)
a. DML is used to manipulate data within the existing
structures (tables) in a database. It focuses on the contents
of the database.
b. Common DML Commands:
i. SELECT: Retrieves data from a table.
ii. INSERT: Adds new rows of data into a table
iii. UPDATE: Modifies existing data in a table.
iv. DELETE: Removes data from a table.
c. Examples
i. SELECT: Retrieves rows from a table.
ii. INSERT: Adds new rows of data to a table.
iii. UPDATE: Modifies existing rows of data.
iv. DELETE: Deletes specific rows of data from a table.
d. Key points
i. DML works with the data within the database.
ii. DML operations can often be rolled back (undone) if
a transaction is not yet committed (depending on
the DBMS and transaction handling).
iii. DML commands do not affect the database schema,
just the data within tables.
3. Why Do Databases Have DDL and DML?
a. Having these two distinct languages allows for a clear
division of roles and tasks in managing a database, making it
more secure, efficient, and maintainable.
3. SQL Syntax and Structure
a. SQL queries generally follow this structure:
ii. Example
ii. Example
1. Create a database for Bookshop: Create a table named Books with columns: book_id,
title, author, year_published, and price.
2. Insert Data into the Books Table: Add at least seven books into the Books table.
3. Query All Books: Write a query to retrieve all information from the Books table.
4. Update Book Price: Change the price of a specific book.
5. Delete a Book: Delete a book from the Books table based on its book_id.
6. Find Books Published After 2010: Retrieve all books published after the year 2010.
7. Submission: Export and upload your output in the Google Drive Link provided.
8. Provide an ERD for this task.