Lab 4 Updated
Lab 4 Updated
Objectives
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.
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
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
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.
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
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
Be sure that the targeted column allows null values by verifying Null? status from the SQL
DESCRIBE command.
INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
Values (7196, ‘GREEN’, ‘SALSEMAN’, 7782, SYSDATE, 2000, NULL, 10);
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.
UPDATE Person SET Address = ‘Stien 12’, City = ‘stavanger’ WHERE LastName =
‘Rasmussen’;
Page 7
4.3.3.1 Delete a Row
"Nina Rasmussen" is going to be deleted:
Page 8