Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Department Computer

CSE Science
of &Engineering

Introduction to SQL: DML01

Lecture 14
CSC 401: Database Management System
Inserting Data
Command: To insert a row of data into a table where a value will be inserted for
every attribute.
INSERT INTO Customer_T VALUES (001, ‘Contemporary Casuals’, ‘1355 S.
Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);

Command: To insert a row of data into a table where some attributes will be left
null.
INSERT INTO Product_T (ProductID, ProductDescription, ProductFinish,
ProductStandardPrice) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8);

Command: Populating a table by using a subset of another table with the same
structure.
INSERT INTO CaCustomer_T SELECT * FROM Customer_T WHERE
CustomerState = ‘CA’;
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Inserting Data
CREATE TABLE Customer_T (
CustomerID INTEGER GENERATED ALWAYS AS IDENTITY (
START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 10000 NO CYCLE),
CustomerName VARCHAR2(25) NOT NULL, CustomerAddress VARCHAR2(30),
CustomerCity VARCHAR2(20), CustomerState CHAR(2),
CustomerPostalCode VARCHAR2(9),
CONSTRAINT Customer_PK PRIMARY KEY (CustomerID);
For Auto generated field the SQL change from
INSERT INTO Customer_T VALUES (001, ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’,
‘Gainesville’,‘FL’, 32601);
to this:
INSERT INTO Customer_T VALUES (‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’,
‘FL’, 32601);
Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Deleting Data
Command: Deleting rows that meet a certain criterion from the
Customer table.
DELETE FROM Customer_T
WHERE CustomerState = ‘HI’;

Command: Deleting all rows from the Customer table.


DELETE FROM Customer_T;

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Updating Data
Command: To modify standard price of product 7 in the Product
table to 775.
UPDATE Product_T
SET ProductStandardPrice = 775
WHERE ProductID = 7;

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
INTERNAL SCHEMA DEFINITION IN RDBMS
Choosing to index primary and/or secondary keys to increase the speed of row selection,

table joining, and row ordering. You can also drop indexes to increase speed of table
updating.
Selecting file organizations for base tables that match the type of processing activity on

those tables.
Selecting file organizations for indexes, which are also tables, appropriate to the way the

indexes are used and allocating extra space for an index file so that an index can grow
without having to be reorganized.
Clustering data so that related rows of frequently joined tables are stored close together in

secondary storage to minimize retrieval time.


Maintaining statistics about tables and their indexes so that the DBMS can find the most

efficient ways to perform various database operations.


Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Index Rules
Indexes are most useful on larger tables.

Specify a unique index for the primary key of each table.

Indexes are most useful for columns that frequently appear in WHERE clauses of SQL

commands either to qualify the rows to select or for linking (joining) tables. In the latter
case, the index is on a foreign key in the OrderLine_T table that is used in joining tables.
Use an index for attributes referenced in ORDER BY (sorting) and GROUP BY

(categorizing) clauses.
Use an index when there is significant variety in the values of an attribute. Oracle suggests

that an index is not useful with fewer than 30 different values and is meaningful with 100 or
more different values for an attribute. Similarly, an index is helpful if the results of a query
do not exceed roughly 20% of the total number of records in the file (Schumacher, 1997).

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Index Rules
Before creating an index on a field with long values, consider first creating a compressed version

of the values (coding the field with a surrogate key) and then indexing on the coded version
(Catterall, 2005). Large indexes, created from long index fields, can be slower to process than
small indexes.
If the key for the index is going to be used for determining the location where the record will be

stored, then the key for this index should be a surrogate key so that the values cause records to be
evenly spread across the storage space (Catterall, 2005). Many DBMSs create a sequence number
so that each new row added to a table is assigned the next number in sequence; this is usually
sufficient for creating a surrogate key.
Check your DBMS for the limit, if any, on the number of indexes allowable per table. If there is

such a limit in your system, you will have to choose those secondary keys that will most likely lead
to improved performance.
Be careful of indexing attributes that have null values.

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Creating Index
Command: To create an alphabetical index on customer name in
the Customer table.
CREATE INDEX Name_IDX ON Customer_T (CustomerName);

Command: To remove the index on the customer name in the


Customer table.
DROP INDEX Name_IDX;

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group
Thank You

Department Computer
CSE Science
of &Engineering CSC 401: database Management System Database Group

You might also like