DATABASE SYSTEM I
DATABASE SYSTEM I LAB CLASS
USING MYSQL-5.5
Instructor Name: Mr. Elias P.
For BAIS 3rd Year Students 2015 EC
Introduction to SQL
•What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard
SQL stands for Structured Query Language.
SQL is a standard language for accessing and manipulating databases.
SQL is a widely used database language, providing means of data
manipulation (store, retrieve, update, delete) and database creation.
Functions of SQL
•What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
Continued
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
However, to be compliant with the ANSI standard, they all support at least
the major commands (such as SELECT, UPDATE, DELETE, INSERT,
WHERE) in a similar manner.
provides a fixed set of data types in particular for strings of different
length char(n), varchar(n), long varchar(n)
SQL Commands
The standard SQL commands to interact with relational databases are CREATE,
SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be
classified into groups based on their nature:
DDL - Data Definition Language
DML - Data Manipulation Language
DQL - Data Query Language
DCL - Data Control Language
TCL -Transaction Control Language
DDL-Data Definition Language
A Data Definition Language (DDL) statement is used to define the database structure
or schema.
 Aim: To study and execute the DDL commands in RDBMS.
DDL commands:
 CREATE
 ALTER
 DROP
 RENAME
 TRUNCATE
Syntax of Commands
CREATE DATABASE
Syntax Create database <database name>
CREATE TABLE
To make a new database, table, index, or stored query. A create statement in SQL creates an object inside of a
relational database management system (RDBMS).
Syntax
CREATE TABLE <table_name>
(
Column_name1 data_type ([size]),
Column_name2 data_type ([size]),
.
.
.
Column_name-n data_type ([size])
);
Continued
DROP A TABLE
Delete Objects from the Database
Syntax DROP TABLE table_name;
TRUNCATE TABLE
Remove all records from a table, including all spaces
allocated for the records are removed.
Syntax TRUNCATE TABLE table_name;
Continued
ALTER A TABLE
To modify an existing database object. Alter the structure of the database.
 To add a column in a table
Syntax
ALTER TABLE table_name ADD column_name datatype;
 To delete a column in a table
Syntax
ALTER TABLE table_name DROP column column_name;
Chapter Three
CONTENTS
Introduction to Data manipulation Language(DML)
Data Manipulation Language Commands
Example on inserting , updating and deleting data from database
Data Manipulation Language(DML)
Continued
By The End of This Session, Students will be able to:
 Explain Data Manipulation Languages
 Identify Data Manipulation Language (DML)Commands Syntax
 Write Data Manipulation Language (DML) Commands when working with database
Introduction to DML
DML stands for Data manipulation Language
Data manipulation language allows the users to query and manipulate
data in existing schema in object.
It allows as to insert data in to the database.
It allows as to delete data from database.
It allows as to update data in the database and recovery data in database
table.
Data Manipulation Language Commands
1.Insert Command
Used to create a record or is used to add new row.
Values can be inserted into table using insert commands. There are two types
of insert commands. They are multiple value insert commands (using ‘&’
symbol) single value insert command (without using ‘&’symbol)
Syntax
INSERT INTO table_name VALUES (value1, value2, value3,…..);
(OR)
INSERT INTO table_name (column1, column2, column3,….) VALUES
(value1,value2,value3,…..);
Continued
2.Update Command
This allows the user to update the particular column value using the where
clause condition.
Syntax
UPDATE <table_name> SET <col1=value> WHERE <column=value>;
3.Delete Command
This allows you to delete the particular column values using where clause
condition.
Syntax
DELETE FROM <table_name> WHERE <condition>;
ACTIVITY
1.Create a table called Employee Information with the following structure
under database Employee.
Name Data Type
Emp_ID Varchar (12), primary key
First_name char (10)
Middle_name char (11)
Last_name char (12)
Phone_num Numeric (10)
age int
DOB date
Salary Decimal (9,4)
Example on inserting, updating and deleting data from
database
2.Insert(Manipulate) the table with the following data
Continued
Emp id First
Name
Middle
Name
Last
Name
Phone
Number
Age Sex DOB Salary
P001 Elias Petros Zegeye 0964887449 23 Male 2000-
09-06
8000
P002 Etagegn Ermias Eda 0934541771 25 Female 1998-
08-07
3000
P003 Zekarias Abel Awel 0932433455 23 Male 2000-
04-08
5000
P004 Minyahil Petros Desse 0916544534 26 Male 1997-
03-05
4000
P005 Sada Amir Husien 0987655634 30 Female 1993-
02-03
9000
Continued
3. Change the first name of employee to Zewdu where employee id is p004.
4.Increase the salary of an employee by 25% for whose salary is less than 4000.
5.Decrease the salary of an employee by 15% for whose salary is greater than 5000
DQL-Data Query Language
COMMAND
SELECT
The select statement is used to query a database .This statement is used to retrieve the
information from the database. The SELECT statement can be used in many ways.
They are:
1.Selecting some columns
To select specified number of columns from the table the Following command is used
Syntax
SELECT column_name FROM table_name;
Continued
2.Query All Columns:
To select all columns from the table * is used instead of column names.
Syntax
SELECT * FROM table_name;
3.Select using DISTINCT:
The DISTINCT keyword is used to return only different values (i.e. ) this command
does not select the duplicate values from the table.
Syntax
SELECT DISTINCT column name(s) FROM table_name;
DCL- Data Control Language
Data Control Language (DCL) statements are used to create roles, permissions, and
referential integrity as well it is used to control access to database by securing it. To
control the data of a database.
Command
GRANT -Gives a privilege to user
REVOKE - withdraw access privileges given with the GRANT command
TCL- Transaction Control Language
Transaction Control (TCL) statements are used to manage the changes made by DML
statements. It allows statements to be grouped together into logical transactions.
Command
COMMIT - save work done
SAVEPOINT - identify a point in a transaction to which you can later roll back
ROLLBACK - restore database to original since the last COMMIT
What is Table?
The data in RDBMS is stored in database objects called tables. The table is a collection
of related data entries and it consists of columns and rows.
Remember, a table is the most common and simplest form of data storage in a
relational database. Following is the example of a CUSTOMERS table
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmadabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
What is Field?
Every table is broken up into smaller entities called fields. The fields in the
CUSTOMERS table consist of ID, NAME, AGE, ADDRESS and SALARY.
A field is a column in a table that is designed to maintain specific information about
every record in the table.
What is record or row?
A record, also called a row of data, is each individual entry that exists in a table. For
example, there are 7 records in the above CUSTOMERS table. Following is a single
row of data or record in the CUSTOMERS table:
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+----+----------+-----+-----------+----------+
A record is a horizontal entity in a table.
What is Column?
A column is a vertical entity in a table that contains all information associated with a
specific field in a table.
For example, a column in the CUSTOMERS table is ADDRESS, which represents
location description and would consist of the following:
+-----------+
| ADDRESS |
+-----------+
| Ahmedabad |
| Delhi |
| Kota |
| Mumbai |
| Bhopal |
| MP |
| Indore |
+----+------+

hjkjlboiupoiuuouoiuoiuoiuoiuoiuoippt.pptx

  • 1.
    DATABASE SYSTEM I DATABASESYSTEM I LAB CLASS USING MYSQL-5.5 Instructor Name: Mr. Elias P. For BAIS 3rd Year Students 2015 EC
  • 2.
    Introduction to SQL •Whatis SQL? SQL stands for Structured Query Language SQL lets you access and manipulate databases SQL is an ANSI (American National Standards Institute) standard SQL stands for Structured Query Language. SQL is a standard language for accessing and manipulating databases. SQL is a widely used database language, providing means of data manipulation (store, retrieve, update, delete) and database creation.
  • 3.
    Functions of SQL •WhatCan SQL do? SQL can execute queries against a database SQL can retrieve data from a database SQL can insert records in a database SQL can update records in a database SQL can delete records from a database SQL can create new databases SQL can create new tables in a database
  • 4.
    Continued SQL can createstored procedures in a database SQL can create views in a database SQL can set permissions on tables, procedures, and views However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner. provides a fixed set of data types in particular for strings of different length char(n), varchar(n), long varchar(n)
  • 5.
    SQL Commands The standardSQL commands to interact with relational databases are CREATE, SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be classified into groups based on their nature: DDL - Data Definition Language DML - Data Manipulation Language DQL - Data Query Language DCL - Data Control Language TCL -Transaction Control Language
  • 6.
    DDL-Data Definition Language AData Definition Language (DDL) statement is used to define the database structure or schema.  Aim: To study and execute the DDL commands in RDBMS. DDL commands:  CREATE  ALTER  DROP  RENAME  TRUNCATE
  • 7.
    Syntax of Commands CREATEDATABASE Syntax Create database <database name> CREATE TABLE To make a new database, table, index, or stored query. A create statement in SQL creates an object inside of a relational database management system (RDBMS). Syntax CREATE TABLE <table_name> ( Column_name1 data_type ([size]), Column_name2 data_type ([size]), . . . Column_name-n data_type ([size]) );
  • 8.
    Continued DROP A TABLE DeleteObjects from the Database Syntax DROP TABLE table_name; TRUNCATE TABLE Remove all records from a table, including all spaces allocated for the records are removed. Syntax TRUNCATE TABLE table_name;
  • 9.
    Continued ALTER A TABLE Tomodify an existing database object. Alter the structure of the database.  To add a column in a table Syntax ALTER TABLE table_name ADD column_name datatype;  To delete a column in a table Syntax ALTER TABLE table_name DROP column column_name;
  • 10.
    Chapter Three CONTENTS Introduction toData manipulation Language(DML) Data Manipulation Language Commands Example on inserting , updating and deleting data from database Data Manipulation Language(DML)
  • 11.
    Continued By The Endof This Session, Students will be able to:  Explain Data Manipulation Languages  Identify Data Manipulation Language (DML)Commands Syntax  Write Data Manipulation Language (DML) Commands when working with database
  • 12.
    Introduction to DML DMLstands for Data manipulation Language Data manipulation language allows the users to query and manipulate data in existing schema in object. It allows as to insert data in to the database. It allows as to delete data from database. It allows as to update data in the database and recovery data in database table.
  • 13.
    Data Manipulation LanguageCommands 1.Insert Command Used to create a record or is used to add new row. Values can be inserted into table using insert commands. There are two types of insert commands. They are multiple value insert commands (using ‘&’ symbol) single value insert command (without using ‘&’symbol) Syntax INSERT INTO table_name VALUES (value1, value2, value3,…..); (OR) INSERT INTO table_name (column1, column2, column3,….) VALUES (value1,value2,value3,…..);
  • 14.
    Continued 2.Update Command This allowsthe user to update the particular column value using the where clause condition. Syntax UPDATE <table_name> SET <col1=value> WHERE <column=value>; 3.Delete Command This allows you to delete the particular column values using where clause condition. Syntax DELETE FROM <table_name> WHERE <condition>;
  • 15.
    ACTIVITY 1.Create a tablecalled Employee Information with the following structure under database Employee. Name Data Type Emp_ID Varchar (12), primary key First_name char (10) Middle_name char (11) Last_name char (12) Phone_num Numeric (10) age int DOB date Salary Decimal (9,4) Example on inserting, updating and deleting data from database
  • 16.
    2.Insert(Manipulate) the tablewith the following data Continued Emp id First Name Middle Name Last Name Phone Number Age Sex DOB Salary P001 Elias Petros Zegeye 0964887449 23 Male 2000- 09-06 8000 P002 Etagegn Ermias Eda 0934541771 25 Female 1998- 08-07 3000 P003 Zekarias Abel Awel 0932433455 23 Male 2000- 04-08 5000 P004 Minyahil Petros Desse 0916544534 26 Male 1997- 03-05 4000 P005 Sada Amir Husien 0987655634 30 Female 1993- 02-03 9000
  • 17.
    Continued 3. Change thefirst name of employee to Zewdu where employee id is p004. 4.Increase the salary of an employee by 25% for whose salary is less than 4000. 5.Decrease the salary of an employee by 15% for whose salary is greater than 5000
  • 18.
    DQL-Data Query Language COMMAND SELECT Theselect statement is used to query a database .This statement is used to retrieve the information from the database. The SELECT statement can be used in many ways. They are: 1.Selecting some columns To select specified number of columns from the table the Following command is used Syntax SELECT column_name FROM table_name;
  • 19.
    Continued 2.Query All Columns: Toselect all columns from the table * is used instead of column names. Syntax SELECT * FROM table_name; 3.Select using DISTINCT: The DISTINCT keyword is used to return only different values (i.e. ) this command does not select the duplicate values from the table. Syntax SELECT DISTINCT column name(s) FROM table_name;
  • 20.
    DCL- Data ControlLanguage Data Control Language (DCL) statements are used to create roles, permissions, and referential integrity as well it is used to control access to database by securing it. To control the data of a database. Command GRANT -Gives a privilege to user REVOKE - withdraw access privileges given with the GRANT command
  • 21.
    TCL- Transaction ControlLanguage Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions. Command COMMIT - save work done SAVEPOINT - identify a point in a transaction to which you can later roll back ROLLBACK - restore database to original since the last COMMIT
  • 22.
    What is Table? Thedata in RDBMS is stored in database objects called tables. The table is a collection of related data entries and it consists of columns and rows. Remember, a table is the most common and simplest form of data storage in a relational database. Following is the example of a CUSTOMERS table +----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmadabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
  • 23.
    What is Field? Everytable is broken up into smaller entities called fields. The fields in the CUSTOMERS table consist of ID, NAME, AGE, ADDRESS and SALARY. A field is a column in a table that is designed to maintain specific information about every record in the table. What is record or row? A record, also called a row of data, is each individual entry that exists in a table. For example, there are 7 records in the above CUSTOMERS table. Following is a single row of data or record in the CUSTOMERS table: +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | +----+----------+-----+-----------+----------+ A record is a horizontal entity in a table.
  • 24.
    What is Column? Acolumn is a vertical entity in a table that contains all information associated with a specific field in a table. For example, a column in the CUSTOMERS table is ADDRESS, which represents location description and would consist of the following: +-----------+ | ADDRESS | +-----------+ | Ahmedabad | | Delhi | | Kota | | Mumbai | | Bhopal | | MP | | Indore | +----+------+