0% found this document useful (0 votes)
36 views71 pages

Database Management System MySQL

Uploaded by

hazelyildiz18
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
36 views71 pages

Database Management System MySQL

Uploaded by

hazelyildiz18
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 71

Database Management

System
Data and Information
• Usually, the terms “data” and “information” are used interchangeably.
However, there is a subtle difference between the two.
• In a nutshell, data can be a number, symbol, character, word, codes,
graphs, etc. On the other hand, information is data put into context.
Information is utilised by humans in some significant way (such as to
make decisions, forecasts etc).
• A basic example of information would be a computer. A computer uses
programming scripts, formulas, or software applications to turn data
into information.
Data Information
Data is unorganised and unrefined facts Information comprises processed, organised data
presented in a meaningful context

Data is an individual unit that contains raw Information is a group of data that collectively
materials which do not carry any specific carries a logical meaning.
meaning.

Data doesn’t depend on information. Information depends on data.

Raw data alone is insufficient for decision making Information is sufficient for decision making

An example of data is a student’s test score The average score of a class is the information
derived from the given data.
What is Data?
Data is a collection of raw, unorganised facts and details like text,
observations, figures, symbols and descriptions of things etc. In other
words, data does not carry any specific purpose and has no significance
by itself. Moreover, data is measured in terms of bits and bytes – which
are basic units of information in the context of computer storage and
processing.

What is Information?
Information is processed, organised and structured data. It provides
context for data and enables decision making. For example, a single
customer’s sale at a restaurant is data – this becomes information when
the business is able to identify the most popular or least popular dish.
How to create table in MYSQL?
The MYSQL CREATE TABLE command is used to create a new table into
the database.

Initially it requires
• Name of the table.
• Names of field(column of the Table).
• Definition of each field(Data Types).
Syntax : CREATE TABLE table_name(column_name column_type);

Example : CREATE TABLE IF NOT EXISTS PRODUCTS


(
prod_id int(11) unsigned not null auto_increment,
prod_code char(4) not null,
name varchar(50) not null,
quantity int(5) unsigned not null default 0,
Price decimal(10,2) not null default 99999.21,
primary key(prod_id)
);
Insert Statement
• Mysql INSERT statement is used to insert data in mysql table within
the database.
• We can insert single or multiple data records using a single query in
mysql.

Syntax: INSERT INTO table_name (field 1,field2,field3, …… field N )


values
(value1,value2,value3 ,………value N);
• If you have all fields data then there is no requirement of determining
or writing fields name we simply insert value and then enter.

• Example: INSERT INTO student VALUES(1,’Prashu


singh’,101,’singh@gmail.com’);

• Example 2: INSERT INTO student (name,email) values (“sonu


rawat”,”sonu@gmail.com”);
Select Statement
The SELECT STATEMENT is used to fetch data from one or more tables
in MySql.
It can retrieve records of all fields or specified fileds.

Syntax for specified field:-


SELECT expressions FROM table_name [where clause] ;
Two ways to fetch particular data:-
SELECT prod_id,name FROM products;
SELECT products.prod_id,products.name FROM products;
Comparison Operator
Operator description

= Equal to

!= Not equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to


Eg1)SELECT products.prod_id,products.name
FROM products
WHERE products.prod_id <=104;

Eg2)SELECT products.name
FROM products
WHERE products.prod_id !=104;
LIKE and NOT LIKE operator
SQL LIKE Operator
The SQL LIKE operator is used with the WHERE clause to get a result set
that matches the given string pattern.

Syntax:
SELECT column1, column2, ...
FROM table
WHERE column LIKE value;
SQL NOT LIKE Operator
We can also invert the working of the LIKE operator by using the NOT operator
with it. This returns a result set that doesn't match the given string pattern.

Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE column NOT LIKE value;
SQL Wildcards
A wildcard character in SQL is used with the LIKE clause to replace a
single character or a set of characters in a string.

% and _ are two commonly used wildcard characters in SQL.


Example:
SELECT * FROM CustomersWHERE country LIKE 'US_’;

Here, _ is a wildcard character that represents exactly one character


after a string.
So, the SQL query selects customers whose country starts with US and
ends with a single character after it.
SELECT *
FROM Customers
WHERE last_name LIKE 'R%’;

Here, % (zero or more characters) is a wildcard. So, the SQL command selects
customers whose last_name starts with R followed by zero or more characters
after it.
Alter statement
• The ALTER TABLE statement in Structured Query Language allows you
to add, modify, and delete columns of an existing table. This
statement also allows database users to add and remove various SQL
constraints on the existing tables.
• Any user can also change the name of the table using this statement.

Syntax: ALTER TABLE table_name ADD new_column_name column


definition [FIRST | AFTER column_name];
Add multiple column in table…
Syntax: ALTER TABLE table_name ADD new_column_name column
definition [FIRST | AFTER column_name] ,
ADD new_column_name column definition [FIRST | AFTER
column_name] ……. N;
Modify column in the table
The modify command is used to modify the column definition of the
table.
Syntax : ALTER TABLE table_name MODIFY column_name
column_definition [ FIRST | AFTER column_name];
Example :
ALTER TABLE products MODIFY name VARCHAR(100) NOT NULL;
Example 2 :
ALTER table users modify password varchar(300) DEFAULT 'bhagvati
kalika';
Rename column
Syntax:
ALTER TABLE table_name CHANGE COLUMN old_name new_name
column definition [FIRST | AFTER column_name];
Example1:
ALTER TABLE products CHANGE COLUMN prod_origin product_origin
VARCHAR(20) NOT NULL;
Example2:
ALTER table users CHANGE COLUMN password pass varchar(100) DEFA
ULT ’ana';
Drop column
This command is used to delete particular column from the table .
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
Example:
ALTER TABLE products DROP COLUMN product_origin;
Example 2:
alter table users drop column hola;
;

Change table name

We can the name of table using RENAME


Syntax:
ALTER TABLE table_name RENAME TO new_table_name;
Example:
ALTER TABLE users RENAME TO allusers;
TRUNCATE TABLE
TRUNCATE statement removes the complete data without removing its
structure.
The TRUNCATE TABLE statement is used when you want to delete the
complete data from a table without removing the table structures.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE table_master;
Foreign Key
a)Foreign key is a key used to link two tables.

b)The table with the foreign key constraint (child table) is connected to
another table(parent table).

c)The connection is between the child table’s foreign key constraint and
the parent Primary key.
d)Foreign key constraints are used to help maintain consistency between the tables.

e)For example:- if a child table record is deleted and the Parent table has
records,the system could also delete the parents records.

f)They also help to prevent entering inaccurate data in the child table by requiring
that a parent table record exists for every record that is entered
In the child table.

Syntax:
CREATE TABLE table_name (…………………….
…………………
…………………..
FOREIGN KEY(column_name) REFERENCES parent_table_name(column_name) );
Syntax 2: ALTER TABLE child_table_name ADD FOREIGN
KEY(column_name)
REFERENCEs parent_table_name(column_name);
Joins
Types of join
select * from student inner join address on student.address
-> = address.a_id inner join fees on student.fees = fees.f_id;
select * from student inner join fee_details on student.fee_amount=
-> fee_details.f_id
-> inner join city on student.city = city.c_id;
Questions……?
Task 1) From the following tables write a SQL query to find the
salesperson and customer who reside in the same city. Return
Salesman, cust_name and city.
function
DELIMITER //
create FUNCTION xyzabc()
RETURNS varchar(50)
BEGIN
RETURN "my name is prashu singh";
End//
Delimiter;
a) How to show existing function in mysql?
SHOW FUNCTION STATUS WHERE DB = ‘database_name’;
b)How to drop function in mysql?
DROP FUNCTION function_name;

Ex1) delimiter //
create function b()
-> returns varchar(20)
-> begin
-> return "prashu singh";
-> end //
Parametrized function
delimiter //
MariaDB [iics]> create function greet(fname varchar(50),lname
varchar(50))
-> returns varchar(150)
-> begin
-> return concat("goodmorning ",fname," ",lname);
-> end //
-> delimiter ;
select greet("kunal","rawat");
Delimiter //
create function marks(mar int)
-> returns varchar(40)
-> begin
-> declare level varchar(50);
-> if mar < 20 then set level = "poor";
-> elseif (mar > 20 and mar < 60)
-> then set level = "moderate";
-> elseif (mar > 60) then set level = "Excellent";
-> end if;
-> return(level);
-> end //
Stored Procedure
call procedure_name; call procedure
SHOW PROCEDURE STATUS WHERE db
= 'your_database_name’; show existing
proceduer
TCL command
A Transaction is a set of SQL statements that are executed on the data
stored in DBMS. Whenever any transaction is made these transactions
are temporarily happen in database

TCL stands for Transaction Control Languages. These commands are


used for maintaining consistency of the database and for the
management of transactions made by the DML commands.

To work with tcl commands use -> set autocommit = “off”;


1. COMMIT :
This command is used to save the data permanently.
Whenever we perform any of the DML command like -INSERT, DELETE
or UPDATE, these can be rollback if the data is not stored permanently.
So in order to be at the safer side COMMIT command is used.

Syntax:

commit;
2. ROLLBACK :
This command is used to get the data or restore the data to the last
savepoint or last committed state. If due to some reasons the data
inserted, deleted or updated is not correct, you can rollback the data to a
particular savepoint or if savepoint is not done, then to the last
committed state.

Syntax:

rollback;

You might also like