Vous êtes sur la page 1sur 8

Lab 4

Creating and Managing Tables

Objectives

After completing this lab, student should be able to do


the following:

1. Naming Rules
2. Data Definition Language:
3. The CREATE TABLE Statement
4. The ALTER TABLE Statement
5. Data Manipulation Language (DML)
6. The INSERT Statement
7. The UPDATE Statement
8. The DELETE Statement
9. The DROP Statement
10. The TRUNCATE Statement

Page 1
4.1 Naming Rules
Name database tables and columns according to the standard rules for naming any Oracle
database object: Table names and column names must begin with a letter and can be 1-30
character long. Names must contain only the characters A-Z, a-z, 0-9, _ (underscore), $ and #
(legal characters, but their use is discouraged). Names must not duplicate the name of another
object owned by the same Oracle Server user. Names must not be an Oracle Server reserved
word.
Naming Guidelines:
Use descriptive names for tables and other database objects. Name the same entity consistently
in different tables. For example, the department number in column is called DEPTNO in both the
EMP table and the DEPT table. Note: Names are case insensitive. For example, EMP is treated
as the same name as eMP or eMp.

4.2 Data Definition Language (DDL)


DDL include CREATE TABLE Statement and ALTER TABLE Statement

4.2.1 The CREATE TABLE Statement


Create tables to store data by executing the SQL CREATE TABLE statement. This statement is
one of the data definition language (DDL) statements. DDL statements are a subset of SQL
statements used to create, modify, or remove Oracle database structures.
These statements have an immediate effect on the database, and they also record information in
the data dictionary. To create a table, a user must have the CREATE TABLE privilege and a
storage area in which to crate objects.

Syntax:
CREATE TABLE table (column datatype[length] ……..);
Table is the name of table. Column is the name of column. Datatype is column’s datatype and
length

CREATE TABLE dept1 (deptno NUMBER(2), dname VARCHAR2(14), loc VARCHAR2(13));

DESCRIBE dept1;

The example above creates the dept1 table, with three columns namely, deptno, dname and loc.
It further confirms the creation of the table by using the DESCRIBE command.

Page 2
Data type Description
CHARACTER(n) Character string. Fixed-length n
VARCHAR(n) Character string. Variable length. Maximum length n
BOOLEAN Stores TRUE or FALSE values
VARBINARY(n) Binary string. Variable length. Maximum length n

INTEGER(p) Integer numerical (no decimal). Precision p


INTEGER Integer numerical (no decimal). Precision 10
DECIMAL(p,s) Exact numerical, precision p, scale s. Example: decimal(5,2) is a
number that has 3 digits before the decimal and 2 digits after the
decimal
NUMERIC(p,s) Exact numerical, precision p, scale s. (Same as DECIMAL)
FLOAT(p) Approximate numerical, mantissa precision p. A floating number in
base 10 exponential notation. The size argument for this type consists
of a single number specifying the minimum precision
FLOAT Approximate numerical, mantissa precision 16
DATE Stores year, month, and day values
TIME Stores hour, minute, and second values
TIMESTAMP Stores year, month, day, hour, minute, and second values
Table 4.1: SQL datatypes

4.2.2 The ALTER TABLE Statement


After you create your tables, you may need to change the table structure because you omitted a
column or your column definition needs to be changed. You can do this by using the ALTER
TABLE statement.

4.2.2.1 Adding a Column


To add a column in a table, use ADD clause.
Syntax:
ALTER TABLE table_name ADD (column_name datatype(size));

ALTER TABLE dept1 ADD (Job varchar2(9));

The example above adds a column named job to the dept1 table. The JOB column becomes the
last column in the table.

Page 3
Note: If a table already contains rows when a column is added, then the new column is initially
null for all the rows.

4.2.2.2 Modifying a Column


You can modify a column definition by using the ALTER TABLE statement with the MODIFY
clause. Column modification can include changes to a column’s datatype, size, and the default
value
Syntax:
ALTER TABLE table_name MODIFY (column_name datatype(size));

ALTER TABLE dept1 MODIFY (ename varchar2(15));

4.2.2.3 Deleting a Column


To delete a column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;

ALTER TABLE dept1 DROP COLUMN job;

4.2.2.4 Renaming a Table


RENAME statement is used to rename a table
Syntax:
RENAME old_name TO new_name;

RENAME dept1 TO department;

Assignments

1. Create the DEPARTMENT table base on the following table instance chart.
Column Name Id Name

Page 4
Datatype Number Varchar2
Length 7 25

2. Create the Employee table base on the following table instance chart.
Column Name ID LAST_NAME FIRST_NAME DEPT_ID
Datatype Number Varchar2 Varchar2 Varchar2
Length 7 25 25 7

3. Modify the EMPLOYEE table to allow for longer employee last names (50).
4. Rename the EMPLOYEE table to EMPLOYEE1

4.3 Data Manipulation Language (DML)


Data manipulation language is a core part of SQL. 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 work is called a transaction. Consider a banking database. When a bank customer
transfers money from a saving account to a checking account, the transaction might consists of
three separate operations: decrease savings account, increase the checking account, and record
the transaction in the transaction journal. The Oracle Server must guarantee that all three SQL
statements are performed to maintain the account in proper balance. When something prevents
one of the statements in the transaction from executing, the other statements of the transaction
must be done.

4.3.1 Adding a New Row to a Table

You can add new rows to a table by issuing the INSERT statement.
Syntax:
INSERT INTO table (column1, column2……column) VALUES (value1, value2,……..valueN);

Note: This statement with the VALUES clause adds only one row at a time to a table.
Insert a new row containing values for each column
List values in the default order of the columns in the table
Optionally list the columns in the INSERT clause
Enclose Character and date values within single quotation marks.

INSERT INTO dept (deptno, dname, loc) VALUES (50, ‘DEVELOPMENT’, DETROIT’);

Page 5
Because you can insert a new row that contains values for each column, the column list is not
required in the INSERT clause. However if you do not use the column list, the values must be
listed according to the default order of the columns in the table.

Method Description
Implicit Omit the column from the column list
Explicit Specify the NULL keyword in the VALUES list specify the empty string (‘ ‘) in
the VALUES list; for character strings and dates only.
Table 4.2: Methods for Inserting Null Values

4.3.1.1 Implicit Method

INSERT INTO dept(deptno, dname) VALUES (60, ‘MIS’);

Here we omitted column location

4.3.1.2 Explicit Method:

INSET INTO dept VALUES (70, ‘FINANCE’, NULL);

Here we have specified the null keyword.

Be sure that the targeted column allows null values by verifying Null? status from the SQL
DESCRIBE command.

4.3.1.3 Inserting Special Values

INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
Values (7196, ‘GREEN’, ‘SALSEMAN’, 7782, SYSDATE, 2000, NULL, 10);

Check your table by using select command.


The above example records the information for employee Green in the EMP table. It supplies the
current date and time in the HIREDATE column. It uses the SYSDATE function for current date
and time.

4.3.1.4 Inserting Specific Date Values

Page 6
The format DD-MM-YY is usually used to insert a date value, with this format, recall that the
century defaults to the current century. Because the date also contains time information, the
default time is midnight (00:00:00).
If a date is required to be entered in a format other than the default (for example, another
century) and/or a specific time is required, use the TO_DATE function.

INSERT INTO emp VALUES (2296, ‘AROMANO’, ‘SALESMAN’, 7782,


TO_DATE(‘FEB 3,97’, ‘MON DD YY’), 100, NULL, 10);

4.3.2 The Update Statement


The UPDATE statement is used to modify the data in a table.
Syntax
UPDATE table_name SET column_name = new_value WHERE column_name= some_value;

4.3.2.1 Update one Column in a Row


We want to add a first name to the person with a last name of "Rasmussen":

UPDATE Person SET FirstName = ‘Nina’ WHERE LastName = ‘Rasmussen’;

4.3.2.2 Update several Columns in a Row


We want to change the address and add the name of the city:

UPDATE Person SET Address = ‘Stien 12’, City = ‘stavanger’ WHERE LastName =
‘Rasmussen’;

4.3.3 The DELETE Statement


The DELETE statement is used to delete rows in a table.
Syntax
DELETE FROM table_name where column_name = some_value;

Page 7
4.3.3.1 Delete a Row
"Nina Rasmussen" is going to be deleted:

DELETE FROM Person WHERE lastName = ‘Rasmussen’;

4.3.3.2 Delete All Rows


It is possible to delete all rows in a table without deleting the table. This means that the table
structure, attributes, and indexes will be intact:

DELETE FROM table table_name;


Or
DELETE * FROM table table_name;

4.3.4 Dropping a Table


The DROP TABLE statement removes the definition of an Oracle table. When you drop a table,
the database loses all the data in the table and all the indexes associated with it.
Syntax:
DROP TABLE table;
(where table is the name of the table)

4.3.5 Truncating a Table


It is used to remove all rows from a table and to release the storage space used by that table.
Syntax:
TRUNCATE TABLE table;

Compiled by Engr. Usman Ali

Page 8

Vous aimerez peut-être aussi