Frequently Asked Questions About Managing Tables and DML Statements in SQL
1.What are DML statements and how do they differ from DDL statements?
DML and DDL are two major types of SQL statements which have somewhat different functions.
DML Statements:
DML statements are used to manipulate the existing database tables data. They can include insertion, updating, deletion and retrieving of data operations. The most commonly used commands of DML include the following:
INSERT. Adds new records in a table.
UPDATE. Changes existing data in a table.
DELETE: Deletes records from a table.
SELECT: Retrieves data from one or more tables (even though it is more commonly thought of as a query statement, it is a DQL).
DML statements modify the contents of the tables but do not change the structure. These statements operate on the actual data within a database that allows for CRUD (Create, Read, Update, Delete).
DDL Statements:
DDL statements are ones that define, alter and maintain the structure of database objects such as tables, indexes, views.
The general DDL commands are:
CREATE: Creates new database objects like tables, views, indexes, etc.
ALTER: The structure of existing database objects can be altered using this command for example, adding or deleting columns in a table.
DROP: Database objects, such as tables or indexes will be deleted using the DROP command.
TRUNCATE: deletes or removes all rows of a table without deleting the table itself, effectively resetting it.
The DDL operations modify the schema or structure of the database. Thus, the changes are usually permanent. Unlike DML the DDL statements automatically commit the changes, which could not be rolled back without a rollback to a prior backup.
Differences
Purpose
The DML is geared toward manipulating the data within tables
The DDL is used to define or alter the structure of the database.
Effect of the change on the database:
DML changes modify data like adding, updating, or deletion of records.
DML changes modify database schema, such as creating or deleting tables.
Transaction Control:
Changes made to DML can be rolled back or committed as these are parts of a transaction.
By default, DDL changes are auto committed and cannot be rolled back.
Changes made to DML can be rolled back or committed as these are parts of a transaction.
By default, DDL changes are auto-committed and cannot be rolled back.
2.How do you insert data into a table using the INSERT INTO statement?
The INSERT INTO statement in SQL is used to add new rows of data into a specific table. There are two ways to use the INSERT INTO statement. Either column names can be mentioned or the values can be inserted without specifying column names. This will allow inserting data into selected columns of a table, so it is easier to handle tables with many columns or with default values. This approach maintains clarity and avoids errors in cases when some columns are optional or have default values. In case the column names are not specified, you need to provide values for all columns as ordered by the schema of a table.
Example 1: Insert Data by Specifying Columns
Consider the Employees table with columns EmpID, EmpName and Department.
To insert a new employee record using this statement, you can use:
INSERT INTO Employees (EmpID, EmpName, Department)
VALUES (1, 'John Doe', 'Sales');
Above, a new row is inserted in the Employees table as follows: EmpID as 1, EmpName as John Doe and Department as Sales.
Example 2: Insert Data Without Specifying Columns
You would insert data without listing column names, which is less common and should be used with caution:
INSERT INTO Employees
VALUES (2, 'Jane Smith', 'Marketing');
Here, the values 2, Jane Smith and Marketing are added directly in the order of the table's schema. This often is unsafe because when a scheme changes in the future, issues arise due to changed-to-be columns. Thus, it would be safer to list column names explicitly. Both methods will suffice, but specifying columns is usually recommended for greater maintainability.
3.How do you insert multiple rows of data into a table at once?
SQL supports inserting multiple rows of data in a table. This is done by using a single 'INSERT INTO' statement, which takes less time than inserting rows individually because there are fewer transactions involved and hence better performance. This general syntax here involves use of the 'INSERT INTO' statement followed by a list of values for each row in a comma separated list.
1. Inserting Multiple Rows in MySQL, PostgreSQL and SQL Server
In these databases, you can insert multiple rows using a single 'INSERT INTO' statement as follows:
INSERT INTO Products (ProductID, ProductName, Price)
VALUES
(1, 'Laptop', 1000.50),
(2, 'Smartphone', 599.99),
(3, 'Tablet', 350.00);
This statement inserts three new rows in the 'Products' table in one statement. It inserts a 'Laptop', a 'Smartphone' and a 'Tablet' with their respective prices.
2. Inserting Multiple Rows in Oracle.
In Oracle, the syntax above is also valid. However, another way is the use of the 'INSERT ALL' statement, mainly when inserting into multiple tables or if the 'VALUES' keyword is not used.
INSERT ALL
INTO Products (ProductID, ProductName, Price) VALUES (4, 'Monitor', 150.75)
INTO Products (ProductID, ProductName, Price) VALUES (5, 'Keyboard', 30.00)
INTO Products (ProductID, ProductName, Price) VALUES (6, 'Mouse', 25.50)
SELECT * FROM DUAL;
This Oracle specific technique 'INSERT ALL' inserts multiple rows effectively in one 'SELECT' statement from 'DUAL', a special table with no rows used in Oracle for such purpose only.
Benefits of Inserting Multiple Rows at Once.
Inserting multiple rows at once can reduce the number of transactions made between the application and the database. This makes the operation faster and more efficient, and it also helps maintain consistency because all the rows are inserted in one go, thus reducing the chance of partial data insertion in case an error occurs.
4.How do you use INSERT INTO with subqueries to insert data?
This could be an efficient way of dynamically populating a table based on data from another table using the INSERT INTO statement and subqueries. This eliminates the workload of doing it manually, since the actual entries may be obtained through a SELECT query against data that satisfies certain conditions. The subquery is the dynamic source of its data this could then really make the process more responsive to real time changes within the database. This is particularly useful in situations where the relationships between data in tables are strong, like filtering or aggregation of rows before being inserted into another table.
Suppose we have two tables Employees with attributes such as EmpID, Name, Department, and Salary and HighEarners which tracks employees whose salary is more than 75000 with attributes EmpID, Name and Salary. The qualifying data for insertion into the HighEarners table could be obtained by something like:
INSERT INTO HighEarners (EmpID, Name, Salary)
SELECT EmpID, Name, Salary
FROM Employees
WHERE Salary > 75000;
5.How do you update existing data in a table using the UPDATE statement?
The SQL UPDATE statement is used to modify existing data in a table. It will allow one or more columns for certain rows to be changed based upon an optional condition as specified in the 'WHERE' clause. Without specifying a WHERE clause then all rows in the table will be updated which can be a dangerous if it wasn't meant. Example: You have a table named Employees with columns EmpID, Name, Department and Salary. You would like to raise the salary of IT department employees by 10%. The Query Would Be:
UPDATE Employees
SET Salary = Salary * 1.1
WHERE Department = 'IT';
In this example the SET clause specifies which column to update ('Salary') and calculates what that new value would be 10% more than the current salary ('Salary * 1.1'). The WHERE clause will limit the update to only those rows whose Department = 'IT'. Without the WHERE clause everyone's salaries would be updated and you likely do not intend this.
Proper use of the UPDATE statement requires a good deal of planning when dealing with huge volumes of data or the critical table. It is normally always quite safe to run such queries over an experimental set of records or use a transaction so that changes can be rolled back in case things go wrong.