0% found this document useful (0 votes)
4 views

Final Pptx

The document discusses handling duplicate data in databases, outlining methods such as INSERT IGNORE, REPLACE INTO, and ON DUPLICATE KEY UPDATE to manage duplicates effectively. It also explains the AUTO_INCREMENT feature in SQL for generating unique primary keys, simplifying data entry, and reducing errors. Additionally, it covers bulk data manipulation techniques for efficient data processing and backup creation using SELECT INTO statements.

Uploaded by

Jojo Bugarin
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)
4 views

Final Pptx

The document discusses handling duplicate data in databases, outlining methods such as INSERT IGNORE, REPLACE INTO, and ON DUPLICATE KEY UPDATE to manage duplicates effectively. It also explains the AUTO_INCREMENT feature in SQL for generating unique primary keys, simplifying data entry, and reducing errors. Additionally, it covers bulk data manipulation techniques for efficient data processing and backup creation using SELECT INTO statements.

Uploaded by

Jojo Bugarin
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/ 34

ADVANCED

DATABASED SYSTEMS
REPORTING
Presented by Group 4
GROUP 4
• Princess Mitchelle De Guzman

• Adrian De Vera

• Andrea Mae Dela Cruz

• John Wayne Diaz


HANDLING
DUPLICATE DATA
• Duplicate data leads to inconsistencies, increased
storage usage, reduced system performance, and
inefficient data processing.
WAYS TO HANDLE
DUPLICATE DATA
USING INSERT IGNORE
Prevents errors – If a duplicate entry (like the same
primary key or unique column) is inserted, MySQL
ignores it instead of throwing an error.

Avoids data duplication – It skips inserting rows that


would cause conflicts.

Useful for bulk inserts – When inserting multiple rows,


it ensures that only the new ones are added without
stopping the whole process
USING INSERT IGNORE
example:
INSERT IGNORE INTO employees (id, name, age)
VALUES (1, 'Alice', 30);

OUTPUT:
• If an employee with id = 1 already exists, the
query will do nothing (no error).

• If id = 1 does not exist, it inserts a new row.


USING REPLACE INTO
• Deletes the existing row with the same primary key
and inserts a new one.
USING REPLACE INTO
example:
REPLACE INTO employees (id, name, age)
VALUES (1, 'Alice', 31);

OUTPUT:

id name age id name age

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?

INSERT IGNORE Ignores the insert (does nothing)

Deletes the existing row & inserts


REPLACE INTO
a new one

INSERT ... ON DUPLICATE KEY


Updates the existing row
UPDATE
What is
AUTO_INCREMENT?
What is
AUTO_INCREMENT?
• Auto Increment is a feature in SQL that allows you to
automatically generate unique values for a column
when new rows are inserted into a table.
What is
AUTO_INCREMENT?
• Auto Increment is a feature in SQL that allows you to
automatically generate unique values for a column
when new rows are inserted into a table.

• It's commonly used to create surrogate keys, such as


primary keys, that are unique identifiers for each row
in a table.
When would you would
use AUTO_INCREMENT
• Create Primary keys: Auto Increment is often used
to generate unique primary key values for a table,
ensuring each row has a unique identifier.
When would you would
use AUTO_INCREMENT
• Create Primary keys: Auto Increment is often used
to generate unique primary key values for a table,
ensuring each row has a unique identifier.
• Avoid Manual Input: It eliminates the need for
manually specifying values for auto-incremented
columns, reducing the risk of errors.
When would you would
use AUTO_INCREMENT
• Create Primary keys: Auto Increment is often used
to generate unique primary key values for a table,
ensuring each row has a unique identifier.
• Avoid Manual Input: It eliminates the need for
manually specifying values for auto-incremented
columns, reducing the risk of errors.
• Simplify Data Entry: It simplifies data entry, as you
don't have to provide a value for the auto-
incremented column when inserting records.
Example query Here's an example SQL query to
create a table named "employees" with an auto-
incremented primary key column "employee_id":
CREATE TABLE employees
( employee_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50) );
HOW MYSQL AUTO-GENERATES
PRIMARY KEY VALUES
example:
• CREATE TABLE employees (id INT AUTO_INCREMENT
PRIMARY KEY, name VARCHAR(50), age INT);

• INSERT INTO employees (name, age) VALUES ('Bob', 28),


('Charlie', 35);

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

• Modifies multiple 1 Alice inactive 2023-12-20

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

1 Alice inactive 2023-12-20

2 Bob active 2024-02-15

3 Charlie active 2024-01-05


UPDATES id name age

• Instead of updating one


row at a time, use CASE 1 Alice 31

for batch updates.


2 Bob 28

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

DELETE FROM employees


WHERE id IN (2, 3); id name age

OUTPUT: 1 Alice 31

4 Emma 29
THANK
You

You might also like