Final Pptx
Final Pptx
DATABASED SYSTEMS
REPORTING
Presented by Group 4
GROUP 4
• Princess Mitchelle De Guzman
• Adrian De Vera
OUTPUT:
• If an employee with id = 1 already exists, the
query will do nothing (no error).
OUTPUT:
1 Andrea 21 1 Alice 31
USING ON DUPLICATE
KEY UPDATE
• Updates the existing row if a duplicate key is
found instead of inserting a new row.
USING ON DUPLICATE KEY UPDATE
example:
INSERT INTO employees (id, name, age)
VALUES (1, 'Alice', 30)
ON DUPLICATE KEY UPDATE age = 31;
OUTPUT:
• If id = 1 exists,
id name age • If id = 1 does not exist, it
inserts a new row.
1 Alice 31
What Happens on Duplicate
Command
Key?
OUTPUT:
id name age
1 Bob 28
2 Charlie 35
Use cases
• Primary Key Generation: To create unique primary keys for
each row in a table, ensuring data integrity and easy
referencing of records.
Use cases
• Primary Key Generation: To create unique primary keys for
each row in a table, ensuring data integrity and easy
referencing of records.
• Simplifying Data Entry: It simplifies data insertion by
automatically generating unique values for a column,
removing the need for manual input.
Use cases
• Primary Key Generation: To create unique primary keys for
each row in a table, ensuring data integrity and easy
referencing of records.
• Simplifying Data Entry: It simplifies data insertion by
automatically generating unique values for a column,
removing the need for manual input.
• Error Reduction: By automatically generating values, it
reduces the risk of errors and ensures that each row has a
unique identifier.
Changing the AUTO_INCREMENT
value
You can use the ‘ALTER TABLE’ statement
to assign a new value to the
AUTO_INCREMENT table option or set the
insert_id server system variable to
change the next AUTO_INCREMENT value
inserted by the current session.
ALTER TABLE animals AUTO_INCREMENT=8;
INSERT INTO animals (name) VALUES ('aardvark’);
EXAMPLE: SELECT * FROM animals;
+----+-----------+
| id | name |
+----+-----------+
| 1 | dog |
| 2 | cat |
| 3 | penguin|
| 4 | fox |
| 5 | whale |
| 6 | ostrich |
| 8 |aardvark|
+----+-----------+
SET insert_id=12;
INSERT INTO animals (name) VALUES ('gorilla’);
SELECT * FROM animals;
+----+-----------+
| id | name |
+----+-----------+
| 1 | dog |
| 2 | cat |
| 3 | penguin|
| 4 | fox |
| 5 | whale |
| 6 | ostrich |
| 8 |aardvark|
| 12 | gorilla |
+----+-----------+
USING SELECT INTO
FOR DATA COPYING
• Copying Data from One Table to Another
• Allows us to quickly create a backup or transfer
data from one table to another.
• It is efficient for making temporary copies,
archiving data, or moving data between databases.
• This method ensures that the copied data retains
its structure while minimizing manual effort.
CREATING BACKUP
TABLES
• The SELECT INTO statement is useful for making
backup copies of tables.
CREATING BACKUP TABLES
Example:
BULK DATA
MANIPULATION
• Involves handling large volumes of data efficiently in
a database.
• It includes inserting, updating, and deleting
multiple records at once to improve performance
and reduce processing time.
• Efficient handling of large data operations can
improve performance
BULK INSERTS WITH
MULTIPLE VALUES
• Instead of inserting rows one by one, use
bulk inserts to improve efficiency.
example:
INSERT INTO employees (id, name, age) VALUES
(3, 'David', 40),
(4, 'Emma', 29);
EFFICIENT BATCH
UPDATES AND DELETES
Updates:
• Instead of updating one row at a time, it
Modifies multiple records based on conditions
or use CASE for batch updates.
Deletes:
• Deleting multiple rows at once reduces
query execution time.
Current:
UPDATES id name status last_login
records based on
2 Bob inactive 2024-02-15
conditions.
3 Charlie inactive 2024-01-05
UPDATE users
SET status = 'active'
WHERE last_login >= OUTPUT:
'2024-01-01';
id name status last_login
3 David 40
UPDATE employees
SET age = CASE
WHEN id = 2 THEN 29 OUTPUT:
WHEN id = 3 THEN 36 id name age
END
WHERE id IN (2, 3);
1 Alice 31
2 Bob 29
3 David 36
id name age
1 Alice 31
2 Bob 28
DELETES
3 David 40
4 Emma 29
OUTPUT: 1 Alice 31
4 Emma 29
THANK
You