DBS211
STRUCTURED QUERY LANGUAGE (SQL)
1
Data Manipulation Language
• When you want to add, update, or delete data in the database, you
execute a DML statement.
• A collection of DML statements that form a logical unit of work is
called a TRANSACTION.
2
The Insert Statement
• Add new rows to a table by using the INSERT statement.
• Only one row is inserted at a time.
INSERT INTO table [(column [, column…])]
VALUES (value [, value…]);
INSERT INTO dbs211_orderdetails (ordernumber, productcode, quantityordered, priceeach, orderlinenumber )
VALUES (10100, 'S24_2022', 55, 44.35, 5);
• Enclose character and date values within single quotation marks.
• The column list is not required.
3
Inserting Rows with Null Values
• Implicit method: Omit the column from the column list.
INSERT INTO dbs211_productlines (productline, textdescription)
VALUES ('Classic Bikes', 'At least 25 years old');
• Explicit method: Specify the NULL keyword.
INSERT INTO dbs211_productlines (productline, textdescription, htmldescription, image)
VALUES ('Classic Bikes', 'At least 25 years old', NULL, NULL);
4
Insert multiple records
• Use the following syntax in order to insert multiple records within one
statement:
INSERT ALL
INTO productlines (productline, textdescription) VALUES('Classic Bikes','At least 25 years old’)
INTO productlines (productline, textdescription) VALUES('Vintage Bike', 'Customized and old’)
SELECT * FROM dual;
5
Inserting Special Values
• Insert special values by using SQL functions
• The SYSDATE function records the current date and time.
• The USER function records the current username.
INSERT INTO dbs211_payments (customernumber, checknumber, paymentdate, amount)
VALUES (103, 'HQ336337', SYSDATE, 5000);
6
Copying Rows from Another Table
• Write your INSERT statement with a subquery.
• Use subquery in place of VALUES.
• Match the number of columns and data types in the INSERT clause to those in the
subquery.
CREATE TABLE dbs211_managers(
employeenumber NUMBER(5) CONSTRAINT dbs211_managers_pk PRIMARY KEY,
lastname VARCHAR2(50),
firstname VARCHAR2(50),
extension VARCHAR2(10) DEFAULT 'NOT KNOWN',
INSERT INTO dbs211_managers
email VARCHAR2(100),
SELECT *
officecode VARCHAR2(10),
FROM dbs211_employees
reportsto NUMBER(5) DEFAULT 1,
WHERE jobtitle LIKE '%Manager%';
jobtitle VARCHAR2(50));
7
Overview of the DEFAULT Constraint
• With the explicit default feature, you can use the DEFAULT keyword as a column value where the
column default is desired.
• This allows the user to control where and when the default value should be applied to data
• Explicit defaults can be used in INSERT and UPDATE statements
INSERT INTO dbs211_managers (employeenumber, firstname)
VALUES(36, 'Farhad');
INSERT INTO dbs211_managers (employeenumber, firstname, extension, reportsto)
VALUES(37, 'Alice', DEFAULT, DEFAULT);
UPDATE dbs211_managers
SET reportsto = DEFAULT
WHERE employeenumber = 36;
8
The UPDATE statement
• You can modify existing rows by using the UPDATE statement.
• You can update more than one row at a time.
• Use the primary key to identify a single row.
• Use WHERE clause to update specific row(s).
UPDATE dbs211_customers
SET firstname = 'Farhad’,
lastname = 'Pourhadi’
WHERE customernumber = 103;
• Omit WHERE clause to update all rows in a table.
UPDATE dbs211_managers
SET reportsto = 1; 9
Integrity Constraint Error
• If you attempt to update a record with a value that is tied to an integrity constraint,
you will get an error.
If Sales Rep Number1703 does not exist, you get an error
UPDATE dbs211_customers
SET salesrepemployeenumber = 1703
WHERE customernumber = 103;
Error report -
ORA-02291: integrity constraint (SYSTEM.CUST_SALESREP_FK) violated - parent key not found
10
The DELETE Statement
• You can remove existing rows by using the DELETE statement.
• Delete specific rows by specifying the WHERE clause.
DELETE FROM dbs211_employees
WHERE employeenumber = 1703;
• Delete all the rows by omitting the WHERE clause
DELETE FROM dbs211_employees;
11
Integrity Constraint Error
• If you attempt to delete a record with a value that is tied to an integrity
constraint, you will get an error.
DELETE FROM dbs211_employees
WHERE employeenumber = 1002;
Error report -
ORA-02292: integrity constraint (SYSTEM.EMP_RTEMP_FK) violated - child record found
12