44
Bordoloi and Bordoloi and Bock Bock Chapter 2 :TABLES AND Chapter 2 :TABLES AND INDEXES INDEXES

Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Embed Size (px)

Citation preview

Page 1: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Chapter 2 :TABLES AND INDEXESChapter 2 :TABLES AND INDEXES

Page 2: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

• One of the first steps in creating a One of the first steps in creating a database is to create the tables that will database is to create the tables that will store organization’s data. store organization’s data.

• In order to create a table , four pieces of In order to create a table , four pieces of information must be determined:information must be determined:

1.1. The table nameThe table name

2.2. The column (field) namesThe column (field) names

3.3. Column data types andColumn data types and

4.4. Column sizesColumn sizes

Table CreationTable Creation

Page 3: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

• Table and Column names should be meaningful Table and Column names should be meaningful and reflect the nature of the data that is to be and reflect the nature of the data that is to be stored.stored.

• If the data stored is about the products that a If the data stored is about the products that a firm sells , then the table should probably be firm sells , then the table should probably be named named product! product!

• If products are identified by a string of eight If products are identified by a string of eight characters, then the column that stores the characters, then the column that stores the product information data should be named product information data should be named product_numberproduct_number, or , or product_code.product_code.

Naming Tables and ColumnsNaming Tables and Columns

Page 4: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Picking a Data typePicking a Data type

• The data type chosen for a column determines The data type chosen for a column determines the nature of the data that can be stored in the the nature of the data that can be stored in the column.column.

• This is termed the This is termed the Domain Domain of valid column of valid column values.values.

• Oracle provides 14 pre-determined data types as Oracle provides 14 pre-determined data types as well as the ability to declare user defined data well as the ability to declare user defined data types.types.

Page 5: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Data Types.Data Types.

• CHAR - Used to store CHAR - Used to store fixed-length, character fixed-length, character data.data.

• VARCHAR2 - Used to store VARCHAR2 - Used to store variable-length, variable-length, character character data that is up to 2000 characters per data that is up to 2000 characters per column entry.column entry.

• LOB – Used to store LOB – Used to store large large object data. A LOB object data. A LOB column can store either character or binary data column can store either character or binary data that is up to four gigabytes in size.that is up to four gigabytes in size.

• LONG – Used to store LONG – Used to store variable-length, variable-length, character character data that is up to two gigabytes in data that is up to two gigabytes in size. size.

contd.contd.

Page 6: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Data TypesData Types

• NUMBER – Used to store NUMBER – Used to store numeric numeric data values data values of a specified precision and scale.of a specified precision and scale.

• DATE – Used to store valid DATE – Used to store valid dates.dates.

Page 7: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

CREATING A TABLECREATING A TABLE

• Creating a simple table that stores five items of Creating a simple table that stores five items of information about employees for an information about employees for an organization.organization.

• The table is named “employee” and stores The table is named “employee” and stores information about each employee’s social information about each employee’s social security number, last name, first name, date security number, last name, first name, date hired, and annual salary. hired, and annual salary.

Contd.Contd.

Page 8: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Table CreationTable Creation

CREATE TABLE employee (CREATE TABLE employee (

emp_ssnemp_ssn CHAR(9),CHAR(9),

emp_last_nameemp_last_name VARCHAR2(25),VARCHAR2(25),

emp_first_nameemp_first_name VARCHAR2(25),VARCHAR2(25),

emp_date_of_birthemp_date_of_birth DATE,DATE,

emp_salaryemp_salary NUMBER(7,2)NUMBER(7,2)

););

• The table name “employee”, is specified The table name “employee”, is specified along with five data columns.along with five data columns.

• Each column has a name that is unique within Each column has a name that is unique within the table and is specified to store a specific the table and is specified to store a specific type of data.type of data.

Page 9: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Data Integrity and Table ConstraintsData Integrity and Table Constraints

• The term data integrity simply means that the The term data integrity simply means that the data stored in the table is valid.data stored in the table is valid.

• There are different types of data integrity, often There are different types of data integrity, often referred to as constraints.referred to as constraints.

• The specifications of different data types aids in The specifications of different data types aids in maintaining certain aspects of the data stored for maintaining certain aspects of the data stored for employees.employees.

Page 10: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

NOT NULL ConstraintNOT NULL Constraint

• A NOT NULL constraint means that a data row A NOT NULL constraint means that a data row must have a value for the column specified as must have a value for the column specified as NOT NULL.NOT NULL.

• A fairly standard practice is to assign each A fairly standard practice is to assign each constraint a constraint a unique constraint name.unique constraint name.

• If constraints are not named, then Oracle assigns If constraints are not named, then Oracle assigns meaningless system-generated names to each meaningless system-generated names to each constraint. constraint.

Page 11: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

ExampleExample

emp_last_name emp_last_name VARCHAR2(25)VARCHAR2(25)

CONSTRAINT nn_emp_last_name NOT NULL,CONSTRAINT nn_emp_last_name NOT NULL,

emp_first_nameemp_first_name VARCHAR2(25)VARCHAR2(25)

CONSTRAINT nn_emp_first_name NOT NULL,CONSTRAINT nn_emp_first_name NOT NULL,

PRIMARY KEY ConstraintPRIMARY KEY Constraint

• Each table must normally contain a column or Each table must normally contain a column or set of columns that uniquely identifies rows of set of columns that uniquely identifies rows of data that are stored in the table. This column or data that are stored in the table. This column or set of columns is referred to as the set of columns is referred to as the primary key.primary key.

Page 12: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

PRIMARY KEY ConstraintPRIMARY KEY Constraint

• If a table requires two or more columns in order If a table requires two or more columns in order to identify each row , the primary key is termed to identify each row , the primary key is termed a a composite composite primary key.primary key.

emp_ssnemp_ssn CHAR(9)CHAR(9)

CONSTRAINT pk_employee CONSTRAINT pk_employee PRIMARY KEY,PRIMARY KEY,

Page 13: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

CHECK ConstraintCHECK Constraint

• Sometimes the data values stored in a specific Sometimes the data values stored in a specific column must fall within some acceptable range column must fall within some acceptable range of values.of values.

• A CHECK constraint is used to enforce this data A CHECK constraint is used to enforce this data limit.limit.emp_salaryemp_salary NUMBER(7,2)NUMBER(7,2) CONSTRAINT ck_emp_salary CONSTRAINT ck_emp_salary CHECK (emp_salary <= 85000), CHECK (emp_salary <= 85000),   

Page 14: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

UNIQUE ConstraintUNIQUE Constraint

• Sometimes it is necessary to enforce uniqueness for a Sometimes it is necessary to enforce uniqueness for a column value that is not primary key column.column value that is not primary key column.

• The The UNIQUE UNIQUE constraint can be used to enforce this constraint can be used to enforce this rule and Oracle rejects any rows that violate the rule and Oracle rejects any rows that violate the constraint.constraint.

• Assume that each parking space for the organization Assume that each parking space for the organization is numbered and that no two employees can be is numbered and that no two employees can be assigned same parking space.assigned same parking space.

emp_parking_spaceemp_parking_space NUMBER(4) NUMBER(4) CONSTRAINT un_emp_parking_space CONSTRAINT un_emp_parking_space

UNIQUE,UNIQUE,

Page 15: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Commands to Manage, Drop and Alter TablesCommands to Manage, Drop and Alter Tables

Viewing a Table DescriptionViewing a Table Description

• The SQL*PLUS DESCRIBE (DESC) command The SQL*PLUS DESCRIBE (DESC) command can display the column names and data types for can display the column names and data types for any table.any table.

• This command can be used when exact data This command can be used when exact data types and column sizes for a table are unknown.types and column sizes for a table are unknown.

Page 16: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

DESCRIBE CommandDESCRIBE Command

DESC employee;DESC employee;  Name Null? TypeName Null? Type------------------- -------- ------------------------------- -------- ------------EMP_SSN NOT NULL CHAR(9)EMP_SSN NOT NULL CHAR(9)EMP_LAST_NAME NOT NULL VARCHAR2(25)EMP_LAST_NAME NOT NULL VARCHAR2(25)EMP_FIRST_NAME NOT NULL VARCHAR2(25)EMP_FIRST_NAME NOT NULL VARCHAR2(25)EMP_DATE_OF_BIRTH DATEEMP_DATE_OF_BIRTH DATEEMP_SALARY NOT NULL NUMBER(7,2)EMP_SALARY NOT NULL NUMBER(7,2)EMP_PARKING_SPACE NUMBER(4)EMP_PARKING_SPACE NUMBER(4)

  

• Note that while Note that while emp_ssn emp_ssn column was specified to have a column was specified to have a PRIMARY KEY constraint, the PRIMARY KEY constraint, the Null? Null? Column displayed Column displayed in the table description indicates whether or not a in the table description indicates whether or not a column is constrained as NOT NULL.column is constrained as NOT NULL.

Page 17: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Dropping a TableDropping a Table

• Employee table can be deleted with the DROP Employee table can be deleted with the DROP TABLE command.TABLE command.

• This command deletes both the table structure, This command deletes both the table structure, its data, related constraints, and indexes.its data, related constraints, and indexes.

DROP TABLE employee;DROP TABLE employee;

Page 18: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Renaming a TableRenaming a Table

• A table can be renamed with the RENAME A table can be renamed with the RENAME command.command.

• This command does not affect table structure or This command does not affect table structure or data; it simply gives the current table a new data; it simply gives the current table a new name.name.

RENAME employee TO worker;RENAME employee TO worker;

Page 19: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Adding and Altering a column for an Adding and Altering a column for an existing Tableexisting Table

• Modifying existing tables to either add Modifying existing tables to either add new columns or alter existing columns can new columns or alter existing columns can be accomplished with the ALTER TABLE be accomplished with the ALTER TABLE MODIFY and ALTER TABLE ADD MODIFY and ALTER TABLE ADD commands.commands.

• The current data type of the The current data type of the emp_parking_space emp_parking_space column is column is NUMBER(4). A very large organization NUMBER(4). A very large organization may have in excess of 9,999 employees. may have in excess of 9,999 employees.

Page 20: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

ALTER TABLE CommandALTER TABLE Command

• The ALTER TABLE command can be used to The ALTER TABLE command can be used to modify the modify the emp_parking_space emp_parking_space column to column to enable the allocation of upto 99,999 parking enable the allocation of upto 99,999 parking spaces.spaces.

ALTER TABLE employee MODIFY ALTER TABLE employee MODIFY (emp_parking_space NUMBER(5));(emp_parking_space NUMBER(5));

• If a column modification will result in a column If a column modification will result in a column that is smaller than was originally specified, that is smaller than was originally specified, Oracle will return an error message if data rows Oracle will return an error message if data rows exist such that their data will not fit into the new exist such that their data will not fit into the new specified column size.specified column size.

Page 21: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

ALTER TABLE CommandALTER TABLE Command

• The ALTER TABLE command can be used to The ALTER TABLE command can be used to add a new column to the employee table.add a new column to the employee table.

• Suppose that an organization recognizes the Suppose that an organization recognizes the need to track the gender of employees in order to need to track the gender of employees in order to meet a governmental reporting requirement, an meet a governmental reporting requirement, an emp_genderemp_gender column can be added to the column can be added to the employee table.employee table.

ALTER TABLE employee ADD ALTER TABLE employee ADD

(emp_gender CHAR(1));(emp_gender CHAR(1));

Page 22: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Relating Tables – Identifying Foreign KeysRelating Tables – Identifying Foreign Keys

• Normally, data rows in one table are related to Normally, data rows in one table are related to data rows in other tables.data rows in other tables.

• The The department department table will store information table will store information about departments within the organization.about departments within the organization.

• Each department has a unique, 2-digit Each department has a unique, 2-digit department number, department name, location, department number, department name, location, and primary telephone number for contacting the and primary telephone number for contacting the department manager. department manager.

Contd.Contd.

Page 23: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Identifying Foreign KeysIdentifying Foreign Keys

CREATE TABLE department (CREATE TABLE department ( dpt_nodpt_no NUMBER(2)NUMBER(2) CONSTRAINT pk_department PRIMARY KEY,CONSTRAINT pk_department PRIMARY KEY, dpt_namedpt_name VARCHAR2(20)VARCHAR2(20) CONSTRAINT nn_dpt_name NOT NULLCONSTRAINT nn_dpt_name NOT NULL););

• Employees are generally assigned to work in Employees are generally assigned to work in departments. In an organization, at a given time, departments. In an organization, at a given time, an employee is assigned to a single department.an employee is assigned to a single department.

• In order to link rows in the In order to link rows in the employee employee table to table to rows in rows in department department table we need to introduce a table we need to introduce a new type of constraint, the FOREIGN KEY new type of constraint, the FOREIGN KEY constraint. constraint.

Page 24: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Identifying Foreign KeysIdentifying Foreign Keys

• Foreign keys (FKs) are columns in one table that Foreign keys (FKs) are columns in one table that reference primary key (PK) values in another or reference primary key (PK) values in another or in the same table.in the same table.

• Lets relate employee rows to the PK column Lets relate employee rows to the PK column named named dpt_no dpt_no in the department table. in the department table.

• The value stored to the emp_dpt_number The value stored to the emp_dpt_number column for any given column for any given employeeemployee row must match row must match a value stored in the a value stored in the dpt_no dpt_no column in the column in the department table.department table.

Page 25: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Identifying Foreign KeysIdentifying Foreign Keys

CREATE TABLE employee (CREATE TABLE employee ( emp_ssn CHAR(9)emp_ssn CHAR(9) CONSTRAINT pk_employee PRIMARY KEY,CONSTRAINT pk_employee PRIMARY KEY, emp_last_name VARCHAR2(25)emp_last_name VARCHAR2(25) CONSTRAINT nn_emp_last_name NOT NULL,CONSTRAINT nn_emp_last_name NOT NULL, emp_first_name VARCHAR2(25)emp_first_name VARCHAR2(25) CONSTRAINT nn_emp_first_name NOT NULL,CONSTRAINT nn_emp_first_name NOT NULL, emp_date_of_birth DATE,emp_date_of_birth DATE, emp_salary NUMBER(7,2)emp_salary NUMBER(7,2) CONSTRAINT ck_emp_salary CONSTRAINT ck_emp_salary CHECK (emp_salary <= 85000),CHECK (emp_salary <= 85000), emp_parking_space NUMBER(4)emp_parking_space NUMBER(4) CONSTRAINT un_emp_parking_space UNIQUE,CONSTRAINT un_emp_parking_space UNIQUE, emp_gender CHAR(1),emp_gender CHAR(1), emp_dpt_number NUMBER(2),emp_dpt_number NUMBER(2),CONSTRAINT fk_emp_dpt FOREIGN KEY (emp_dpt_number)CONSTRAINT fk_emp_dpt FOREIGN KEY (emp_dpt_number) REFERENCES department ON DELETE SET NULLREFERENCES department ON DELETE SET NULL););

Page 26: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

MAINTAINING REFERRENTIAL INTEGRITYMAINTAINING REFERRENTIAL INTEGRITY

• FOREIGN KEY constraints are also referred to FOREIGN KEY constraints are also referred to as referential integrity constraints, and assist in as referential integrity constraints, and assist in maintaining database integrity.maintaining database integrity.

• Referential integrity stipulates that values of a Referential integrity stipulates that values of a foreign key must correspond to values of a foreign key must correspond to values of a primary key in the table that it references. But primary key in the table that it references. But what happens if the primary key values change what happens if the primary key values change or the row that is referenced is deleted? or the row that is referenced is deleted?

Page 27: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

MAINTAINING REFERRENTIAL INTEGRITYMAINTAINING REFERRENTIAL INTEGRITY

• Several methods exist to ensure that referential Several methods exist to ensure that referential integrity is maintained.integrity is maintained.

• ON DELETE SET NULL clause can be used to ON DELETE SET NULL clause can be used to specify that the value of specify that the value of emp_dpt_number emp_dpt_number should be set to null if the referenced should be set to null if the referenced department department row is deleted.row is deleted.

• There are additional referential integrity There are additional referential integrity constraints that can be used to enforce database constraints that can be used to enforce database integrity. integrity.

Page 28: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

MAINTAINING REFERRENTIAL INTEGRITYMAINTAINING REFERRENTIAL INTEGRITY

On Update (Delete) Restrict

Any update/delete made to the department table that would delete or change a primary key value will be rejected unless no foreign key references that value in the employee table. This is the default constraint in Oracle.

On Update (Delete) Cascade

Any update/delete made to the department table should be cascaded through to the employee table.

On Update (Delete) Set Null

Any values that are updated/deleted in the department table cause affected columns in the employee table to be set to null.

Page 29: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

THE INSERT CommandTHE INSERT Command

• The INSERT command is used to store data in The INSERT command is used to store data in tables.tables.

• The INSERT command is often embedded in The INSERT command is often embedded in higher-level programming language applications higher-level programming language applications as an embedded SQL command.as an embedded SQL command.

• There are two different forms of the INSERT There are two different forms of the INSERT command.command.

• The first form is used if a new row will have a The first form is used if a new row will have a value inserted into each column of the row. value inserted into each column of the row.

Page 30: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

THE INSERT CommandTHE INSERT Command

• The general form of the INSERT command is The general form of the INSERT command is

INSERT INTO tableINSERT INTO table

VALUES (column1 value, column2 VALUES (column1 value, column2 value, …);value, …);

• The second form of the INSERT command is The second form of the INSERT command is used to insert rows where some of the column used to insert rows where some of the column

data is unknown (NULLdata is unknown (NULL). ).

Page 31: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

THE INSERT CommandTHE INSERT Command

• This form of the INSERT command requires This form of the INSERT command requires that you specify the names of the columns for that you specify the names of the columns for which data are being stored.which data are being stored.

INSERT INTO employee (emp_ssn, INSERT INTO employee (emp_ssn, emp_last_name, emp_first_name)emp_last_name, emp_first_name)

VALUES ('999111111', 'Bock', VALUES ('999111111', 'Bock', 'Douglas');'Douglas');

  

Page 32: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

The DELETE CommandThe DELETE Command

• The DELETE command is perhaps the The DELETE command is perhaps the simplest of the SQL statements. simplest of the SQL statements.

• It removes one or more rows from a table. It removes one or more rows from a table. Multiple table delete operations are not Multiple table delete operations are not allowed in SQL. allowed in SQL.

• The syntax of the DELETE command is:The syntax of the DELETE command is:

DELETE FROM table_nameDELETE FROM table_name

[WHERE [WHERE conditioncondition];];

Page 33: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

The DELETE CommandThe DELETE Command

• Since the WHERE clause is optional, you can Since the WHERE clause is optional, you can easily delete all rows from a table by omitting easily delete all rows from a table by omitting a WHERE clause since the WHERE clause a WHERE clause since the WHERE clause limits the scope of the DELETE operation.limits the scope of the DELETE operation.

• For example, the DELETE FROM command For example, the DELETE FROM command shown here removes all rows in the shown here removes all rows in the assignmentassignment table. table.

DELETE FROM assignment;DELETE FROM assignment;

  

Page 34: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

The UPDATE CommandThe UPDATE Command

• Values stored in individual columns of Values stored in individual columns of selected rows can be modified (updated) with selected rows can be modified (updated) with the UPDATE command. the UPDATE command.

• Updating Updating columnscolumns is different from is different from alteringaltering columns. columns.

• The ALTER command changes the table The ALTER command changes the table structure, but leaves the table data unaffected.structure, but leaves the table data unaffected.

• The UPDATE command changes data in the The UPDATE command changes data in the table, not the table structure. table, not the table structure.

Page 35: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

The UPDATE CommandThe UPDATE Command

• The general syntax of the UPDATE The general syntax of the UPDATE command is: command is:

UPDATE UPDATE tabletable

SET SET columncolumn = = expressionexpression [, [,columncolumn = = expressionexpression]...]...

[WHERE [WHERE conditioncondition];];

Page 36: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

The COMMIT CommandThe COMMIT Command

• INSERT, UPDATE, and DELETE commands INSERT, UPDATE, and DELETE commands are not committed to the database until the are not committed to the database until the COMMIT statement is executed.COMMIT statement is executed.

• COMMIT is a transaction managing COMMIT is a transaction managing command that confirms operations to the command that confirms operations to the database on the server (closing Oracle also database on the server (closing Oracle also acts as a confirmation of the commands acts as a confirmation of the commands entered). entered).

Page 37: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

The ROLLBACK CommandThe ROLLBACK Command

• SQL command ROLLBACK (ROLL) can be SQL command ROLLBACK (ROLL) can be issued immediately to cancel any database issued immediately to cancel any database operations since the most recent COMMIT.operations since the most recent COMMIT.

• Like COMMIT, ROLLBACK is also a Like COMMIT, ROLLBACK is also a transaction managing command; however, it transaction managing command; however, it cancels operations instead of confirming cancels operations instead of confirming them. them.

Page 38: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

INDEXESINDEXES

• Indexes are optional structures associated Indexes are optional structures associated with tables. with tables.

• There are different types of indexes including There are different types of indexes including those used to enforce primary key constraints, those used to enforce primary key constraints, unique indexes, non-unique indexes, unique indexes, non-unique indexes, concatenated indexes, and others. concatenated indexes, and others.

Page 39: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

PRIMARY KEY indexesPRIMARY KEY indexes

• When a PRIMARY KEY constraint is When a PRIMARY KEY constraint is specified, Oracle will automatically create a specified, Oracle will automatically create a unique index to support rapid data retrieval unique index to support rapid data retrieval for the specified table.for the specified table.

• Without an index, a command that retrieves Without an index, a command that retrieves data will cause Oracle to completely scan a data will cause Oracle to completely scan a table for rows that satisfy the retrieval table for rows that satisfy the retrieval condition.condition.

• For example, consider the following SELECT For example, consider the following SELECT statement that will display a list of employees statement that will display a list of employees assigned to a specific department.assigned to a specific department.

Page 40: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

PRIMARY KEY indexesPRIMARY KEY indexes

SELECT emp_last_name,emp_first_name, SELECT emp_last_name,emp_first_name, emp_dpt_number FROM employeeemp_dpt_number FROM employee

WHERE emp_dpt_number = 7;WHERE emp_dpt_number = 7;

• If the If the employeeemployee table is not indexed on the table is not indexed on the emp_dpt_numberemp_dpt_number column, Oracle will have to column, Oracle will have to scan the entire table in order to satisfy the scan the entire table in order to satisfy the query. query.

Page 41: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Creating an IndexCreating an Index

• The general form of the CREATE INDEX The general form of the CREATE INDEX command is:command is:

CREATE INDEX <index name> CREATE INDEX <index name>

ON <table name> (column1, column2…);ON <table name> (column1, column2…);

Page 42: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

Creating a UNIQUE IndexCreating a UNIQUE Index

• The general form of the CREATE UNIQUE The general form of the CREATE UNIQUE INDEX command is:INDEX command is:

CREATE UNIQUE INDEX <index name> CREATE UNIQUE INDEX <index name>

ON <table name> (column1, column2…);ON <table name> (column1, column2…);

Page 43: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

DROPPING IndexesDROPPING Indexes

• An index should not be retained unless it An index should not be retained unless it improves system processing in some fashion.improves system processing in some fashion.

• All indexes on a table must be updated All indexes on a table must be updated whenever row data is changed that is whenever row data is changed that is referenced by an index.referenced by an index.

• Useless indexes burden the system by adding Useless indexes burden the system by adding unnecessary maintenance and by needlessly unnecessary maintenance and by needlessly occupying disk space. These indexes should occupying disk space. These indexes should be dropped. be dropped.

Page 44: Bordoloi and Bock Chapter 2 :TABLES AND INDEXES. Bordoloi and Bock One of the first steps in creating a database is to create the tables that will store

Bordoloi and BockBordoloi and Bock

DROPPING IndexesDROPPING Indexes

• The syntax of the DROP INDEX command is The syntax of the DROP INDEX command is simple:simple:

DROP INDEX index_name;DROP INDEX index_name;