?? Day 32: Exploring Practical SQL Implementation - Part 4??
JIGNESH KUMAR
Electrical & Instrumentation Engineer at SIC | Data Science Enthusiastic | ICE' 2024 SLIET
A. Modifying SQL Commands used in Database:
When working with databases, it is essential to have a good understanding of modifying SQL commands to efficiently manage data. Here are some key commands for modifying data in a database:
1. Insert Data:???- The INSERT INTO command is used to add new records to a table.???
2. Insert from a Table:???- You can insert data into a table from another table using the INSERT INTO SELECT statement.???
3. Insert Multiple Rows:???- To insert multiple rows at once, you can use the INSERT INTO command with multiple value sets.??
4. Update:???- The UPDATE command is used to modify existing records in a table.??
5. Update with Join:???- You can update records in one table based on values from another table using the UPDATE JOIN statement.???
6. Delete:???- The DELETE FROM command is used to remove records from a table.??
7. Alter Table: The ALTER TABLE statement in SQL is a powerful command used to modify the structure of an existing table. It allows you to add, modify, or drop columns, as well as perform other structural changes.
By mastering these SQL commands for modifying data in a database, you can effectively manage and manipulate the information stored in your database tables.
Real Life-Example
1. Insert Data:
-- Scenario: Adding a new book to the catalog
INSERT INTO books (title, author, price)
VALUES ('The Art of SQL', 'Stephane Faroult', 984);
2. Insert from a Table:
-- Scenario: Adding new books from a supplier's catalog
INSERT INTO books (title, author, price)
SELECT title, author, price
FROM supplier_catalog
WHERE category = 'Technology';
3. Insert Multiple Rows:
-- Scenario: Introducing a book bundle promotion
INSERT INTO books (title, author, price)
VALUES
('Data Science Essentials', 'Jake VanderPlas', 941),
('Machine Learning Mastery', 'Jason Brownlee', 732),
('Python Crash Course', 'Eric Matthes', 704);
4. Update:
-- Scenario: Adjusting the price of a specific book
UPDATE books
SET price = 785
WHERE title = 'Python Crash Course';
5. Update with Join:
-- Scenario: Updating book ratings based on customer reviews
UPDATE books b
SET b.rating = r.average_rating
FROM reviews r
WHERE b.book_id = r.book_id;
6. Delete:
-- Scenario: Removing a book that is no longer available
DELETE FROM books
WHERE title = 'Outdated Technology Trends';
7. Alter Table:
-- Adding a new column 'registration_date' to the 'customers' table
ALTER TABLE customers
ADD COLUMN registration_date DATE;
领英推荐
B. Indexing SQL Commands used in Database:
Indexes in a database are crucial for optimizing query performance by providing faster access to data. Here are some key SQL commands related to indexes:
1. Create Index:???- The CREATE INDEX command is used to create an index on one or more columns in a table.???
2. Unique Index:???- You can create a unique index to enforce uniqueness on one or more columns in a table.???
3. Drop Index:???- The DROP INDEX command is used to remove an existing index from a table.??
4. Clustered Index:???- A clustered index determines the physical order of data rows in a table. You can create a clustered index using the CLUSTERED keyword.???
5. Non-Clustered Index:???- A non-clustered index is a separate structure from the table data and can be created using the NONCLUSTERED keyword.??
6. Composite Index:???- You can create an index on multiple columns to improve query performance for specific queries that involve those columns.?
By utilizing these SQL commands for indexes, you can enhance the efficiency of database queries and improve overall performance when working with large datasets.
Real Life-Example
1. Create Index:
-- Scenario: Improving search performance for book titles
CREATE INDEX idx_title ON books (title);
2. Unique Index:
-- Scenario: Ensuring uniqueness in employee IDs
CREATE UNIQUE INDEX idx_employee_id ON employees (employee_id);
3. Drop Index:
-- Scenario: Removing an unnecessary index
DROP INDEX idx_unused_index;
4. Clustered Index:
-- Scenario: Clustering books based on publication year
CREATE CLUSTERED INDEX idx_year_cluster ON books (publication_year);
5. Non-Clustered Index:
-- Scenario: Improving search performance for author names
CREATE NONCLUSTERED INDEX idx_author_search ON books (author);
6. Composite Index:
-- Scenario: Enhancing search speed for combined criteria
CREATE INDEX idx_combined_search ON orders (customer_id, order_date);
??AI-Employees for Support & Lead Gen ??AI-Powered Marketing Software & Service??$99 No Code App Builder ??$99 Social Media ??Sales & Marketing Automation ????FB & LinkedIn Marketing ??Founder @NexLvL CRM & Apps
9 个月Impressive SQL tips! ?? Which SQL command offers the most robust database enhancement?