Database Objects and Creating Tables

 
S
Q
L
C
R
E
A
T
I
N
G
 
A
N
D
 
M
A
N
A
G
I
N
G
 
T
A
B
L
E
S
 
l
e
c
t
u
r
e
4
 
1
 
D
a
t
a
b
a
s
e
 
O
b
j
e
c
t
s
D
a
t
a
b
a
s
e
 
O
b
j
e
c
t
s
 
D
a
t
a
b
a
s
e
 
O
b
j
e
c
t
s
 
All the previews database objects can be created using
CREATE 
sql statement
 
 
T
a
b
l
e
s
 
Creating tables
The alter statement
Dropping table
 
C
r
e
a
t
i
n
g
 
t
a
b
l
e
:
 
S
y
n
t
a
x
 
D
a
t
a
b
a
s
e
 
t
a
b
l
e
s
 
c
r
e
a
t
e
d
 
u
s
i
n
g
 
(
C
R
E
A
T
E
 
T
A
B
L
E
s
t
a
t
e
m
e
n
t
)
T
h
e
 
M
i
n
i
m
a
l
 
S
y
n
t
a
x
 
i
s
:
 
 
 
Y
o
u
 
S
p
e
c
i
f
y
:
t
a
b
l
e
n
a
m
e
 
:
 
T
h
e
 
n
a
m
e
 
y
o
u
 
c
h
o
o
s
e
s
 
f
o
r
 
t
h
e
 
t
a
b
l
e
c
o
l
u
m
n
 
:
 
C
o
l
u
m
n
 
n
a
m
e
d
a
t
a
 
t
y
p
e
 
:
 
c
o
l
u
m
n
 
d
a
t
a
 
t
y
p
e
 
 
w
h
i
c
h
 
s
p
e
c
i
f
i
e
s
 
t
h
e
 
t
y
p
e
 
o
f
 
d
a
t
a
 
t
o
 
b
e
 
s
t
o
r
e
d
 
i
n
 
t
h
i
s
 
c
o
l
u
m
n
 
a
n
d
 
 
c
o
l
u
m
n
s
i
z
e
D
E
F
A
U
L
T
 
e
x
p
r
:
 
s
p
e
c
i
f
i
e
s
 
a
 
d
e
f
a
u
l
t
 
v
a
l
u
e
 
i
f
 
a
 
v
a
l
u
e
 
i
s
 
o
m
i
t
t
e
d
 
i
n
 
t
h
e
 
 
I
N
S
E
R
T
 
s
t
a
t
e
m
e
n
t
C
R
E
A
T
E
 
 
T
A
B
L
E
 
 
 
t
a
b
l
e
n
a
m
e
 
(
c
o
l
u
m
n
 
d
a
t
a
 
t
y
p
e
 
[
D
E
F
A
U
L
T
 
e
x
p
r
]
,
 
.
.
)
;
 
C
r
e
a
t
i
n
g
 
t
a
b
l
e
:
 
N
a
m
i
n
g
 
R
u
l
e
s
 
T
a
b
l
e
 
n
a
m
e
s
 
a
n
d
 
c
o
l
u
m
n
 
n
a
m
e
s
:
Must begin with a letter
Must be 1 to 30 characters long
Must contain only A–Z, a–z, 0–9, _, $, and #
M
u
s
t
 
n
o
t
 
d
u
p
l
i
c
a
t
e
 
t
h
e
 
n
a
m
e
 
o
f
 
a
n
o
t
h
e
r
 
o
b
j
e
c
t
 
o
w
n
e
d
 
b
y
 
t
h
e
 
s
a
m
e
u
s
e
r
Must not be an Oracle Server reserved word
N
o
t
e
:
 
N
a
m
e
s
 
a
r
e
 
n
o
t
 
c
a
s
e
 
s
e
n
s
i
t
i
v
e
.
 
(
F
o
r
 
e
x
a
m
p
l
e
,
E
M
P
L
O
Y
E
E
S
 
i
s
 
t
r
e
a
t
e
d
 
a
s
 
t
h
e
 
s
a
m
e
 
n
a
m
e
 
a
s
 
e
M
P
l
o
y
e
e
s
o
r
 
e
M
p
L
O
Y
E
E
S
.
)
 
 
C
r
e
a
t
i
n
g
 
t
a
b
l
e
:
 
N
a
m
i
n
g
 
G
u
i
d
e
l
i
n
e
 
T
a
b
l
e
 
n
a
m
e
s
 
a
n
d
 
c
o
l
u
m
n
 
n
a
m
e
s
:
Use a descriptive names for table as well the other database objects
N
a
m
e
s
 
a
r
e
 
n
o
t
 
c
a
s
e
 
s
e
n
s
i
t
i
v
e
.
 
(
F
o
r
 
e
x
a
m
p
l
e
,
 
E
M
P
L
O
Y
E
E
S
 
i
s
t
r
e
a
t
e
d
 
a
s
 
t
h
e
 
s
a
m
e
 
n
a
m
e
 
a
s
 
e
M
P
l
o
y
e
e
s
 
o
r
 
e
M
p
L
O
Y
E
E
S
.
)
 
 
 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2
)
,
 
d
n
a
m
e
V
A
R
C
H
A
R
2
(
1
4
)
,
d
l
o
c
a
t
i
o
n
 
V
A
R
C
H
A
R
2
(
1
3
)
)
;
Table created 
 
Appears when you run the query
D
E
S
C
R
I
B
E
 
d
e
p
t
 
;
 
 
 
 
 
C
r
e
t
e
 
a
 
T
a
b
l
e
 
T
o
 
C
o
n
f
i
r
m
 
t
h
e
 
c
r
e
a
t
i
o
n
 
T
h
e
 
A
L
T
E
R
 
T
A
B
L
E
 
S
t
a
t
e
m
e
n
t
 
U
s
e
 
t
h
e
 
A
L
T
E
R
 
T
A
B
L
E
 
s
t
a
t
e
m
e
n
t
 
t
o
:
 
Add a new column
• Modify an existing column
• Define a default value for the new column
• Drop a column
 
T
h
e
 
A
L
T
E
R
 
T
A
B
L
E
 
S
t
a
t
e
m
e
n
t
:
 
S
y
n
t
a
x
 
U
s
e
 
t
h
e
 
A
L
T
E
R
 
T
A
B
L
E
 
s
t
a
t
e
m
e
n
t
 
t
o
 
a
d
d
,
 
m
o
d
i
f
y
 
o
r
 
d
r
o
p
c
o
l
u
m
n
s
.
A
L
T
E
R
 
T
A
B
L
E
 
t
a
b
l
e
 
 
 
 
 
 
 
 
 
 
A
D
D
 
(
c
o
l
u
m
n
 
d
a
t
a
t
y
p
e
 
[
D
E
F
A
U
L
T
 
e
x
p
r
]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[
,
 
c
o
l
u
m
n
 
d
a
t
a
t
y
p
e
]
.
.
.
)
;
A
L
T
E
R
 
T
A
B
L
E
 
t
a
b
l
e
 
 
 
 
 
 
 
 
 
M
O
D
I
F
Y
 
(
c
o
l
u
m
n
 
d
a
t
a
t
y
p
e
 
[
D
E
F
A
U
L
T
 
e
x
p
r
]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
[
,
 
c
o
l
u
m
n
 
d
a
t
a
t
y
p
e
]
.
.
.
)
;
A
L
T
E
R
 
T
A
B
L
E
 
t
a
b
l
e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
D
R
O
P
 
C
O
L
U
M
N
 
 
 
c
o
l
u
m
n
;
 
T
h
e
 
A
L
T
E
R
 
T
A
B
L
E
 
S
t
a
t
e
m
e
n
t
:
 
S
y
n
t
a
x
 
T
h
e
 
A
L
T
E
R
 
T
A
B
L
E
 
S
t
a
t
e
m
e
n
t
:
 
E
x
a
m
p
l
e
(
A
d
d
)
 
Use the ADD clause to add columns.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
A
L
T
E
R
 
T
A
B
L
E
 
d
e
p
t
8
0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
A
D
D
 
(
j
o
b
_
i
d
 
V
A
R
C
H
A
R
2
(
9
)
)
;
 
•Note That
You cannot specify where the new column appear
 
The new column
becomes the 
last column 
directly.
If the table already contains rows when the new column added, then
the new column is initialized to NULL (as in this example) or  default
value (if specified) for all the rows
 
T
h
e
 
A
L
T
E
R
 
T
A
B
L
E
 
S
t
a
t
e
m
e
n
t
:
 
E
x
a
m
p
l
e
(
A
d
d
)
 
T
h
e
 
A
L
T
E
R
 
T
A
B
L
E
 
S
t
a
t
e
m
e
n
t
:
 
E
x
a
m
p
l
e
(
M
o
d
i
f
y
)
 
You can change a column’s 
data type
, 
size
, and
 
 
 
 
d
e
f
a
u
l
t
 
v
a
l
u
e
.
 
A
L
T
E
R
 
T
A
B
L
E
 
d
e
p
t
8
0
 
 
 
 
 
 
 
 
 
 
M
O
D
I
F
Y
 
(
l
a
s
t
_
n
a
m
e
 
V
A
R
C
H
A
R
2
(
3
0
)
)
;
 
 
N
o
t
e
 
T
h
a
t
:
A
 
c
h
a
n
g
e
 
t
o
 
t
h
e
 
d
e
f
a
u
l
t
 
v
a
l
u
e
 
a
f
f
e
c
t
s
 
o
n
l
y
 
s
u
b
s
e
q
u
e
n
t
 
i
n
s
e
r
t
i
o
n
s
 
t
o
t
h
e
 
t
a
b
l
e
You can decrease the width of a column only if the column contains only
null values or if the table has no rows.
You can change the data type only if the column contains null values
.
 
T
h
e
 
A
L
T
E
R
 
T
A
B
L
E
 
S
t
a
t
e
m
e
n
t
:
 
E
x
a
m
p
l
e
(
D
R
O
P
)
 
Use the DROP COLUMN clause to drop columns you no
longer need from the table.
A
L
T
E
R
 
T
A
B
L
E
 
d
e
p
t
8
0
 
 
 
 
 
 
 
 
 
 
D
R
O
P
 
 
C
O
L
U
M
N
 
 
 
j
o
b
_
i
d
;
• Note that:
The column may or may not contain data.
Using the ALTER TABLE statement, only one column can be
dropped at a time.
The table must have at least one column remaining in it after it is
altered.
Once a column is dropped, it cannot be recovered
 
D
r
o
p
p
i
n
g
 
a
 
T
a
b
l
e
 
• All 
data
 and 
structure
 in the table is deleted.
• You cannot roll back the DROP TABLE statement.
 
D
R
O
P
 
T
A
B
L
E
 
 
d
e
p
t
8
0
;
 
T
r
u
n
c
a
t
i
n
g
 
a
 
T
a
b
l
e
 
• The TRUNCATE TABLE statement:
Removes all rows from a table
Releases the storage space used by that table
 
T
R
U
N
C
A
T
E
 
 
T
A
B
L
E
 
 
d
e
t
a
i
l
_
d
e
p
t
;
 
Note That
Y
o
u
 
c
a
n
n
o
t
 
r
o
l
l
 
b
a
c
k
 
r
o
w
 
r
e
m
o
v
a
l
 
w
h
e
n
 
u
s
i
n
g
 
 
T
R
U
N
C
A
T
E
.
A
l
t
e
r
n
a
t
i
v
e
l
y
,
 
y
o
u
 
c
a
n
 
r
e
m
o
v
e
 
r
o
w
s
 
b
y
 
u
s
i
n
g
 
t
h
e
 
D
E
L
E
T
E
 
s
t
a
t
e
m
e
n
t
.
 
(
L
a
t
e
r
)
If the table is the parent of a referential integrity constraint, you cannot
truncate the table. Disable the constraint before issuing the TRUNCATE
statement.
 
 
E
x
e
r
s
i
s
e
s
 
. Create the DEPT table based on the following table instance
chart. Place the syntax in a script called lab_1.sql, then execute
the statement in the script to create the table.
 
 
 
 
Confirm that the table is created.
Modify the DEPT table to allow for longer employee last names.
Confirm your modification
Empty the table.
Slide Note
Embed
Share

Database objects like tables, indexes, and sequences are essential components in SQL databases. Learn how to create and manage tables efficiently using SQL statements. Understand the syntax, naming rules, guidelines, and data types while creating tables in a database system.

  • Database Objects
  • Creating Tables
  • SQL Syntax
  • Data Types
  • Naming Rules

Uploaded on Feb 15, 2025 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 1 SQL CREATING AND MANAGING TABLES lecture4

  2. Database Objects Object Description Database Objects Table Basic unit of storage; composed of rows and columns View Logically represents subsets of data from one or more tables Sequence Numeric value generator Index Improves the performance of some queries Synonym Gives alternative names to the other objects

  3. Database Objects All the previews database objects can be created using CREATE sql statement

  4. Tables Creating tables The alter statement Dropping table

  5. Creating table: Syntax Database tables created using (CREATE TABLE statement) The Minimal Syntax is: CREATE TABLE tablename (column data type [DEFAULT expr], ..); You Specify: tablename : The name you chooses for the table column : Column name data type : column data type which specifies the type of data to be stored in this column and column size DEFAULT expr: specifies a default value if a value is omitted in the INSERT statement

  6. Creating table: Naming Rules Table names and column names: Must begin with a letter Must be 1 to 30 characters long Must contain only A Z, a z, 0 9, _, $, and # Must not duplicate the name of another object owned by the same user Must not be an Oracle Server reserved word Note: Names are not case sensitive. (For example, EMPLOYEES is treated as the same name as eMPloyees or eMpLOYEES.)

  7. Creating table: Naming Guideline Table names and column names: Use a descriptive names for table as well the other database objects Names are not case sensitive. (For example, EMPLOYEES is treated as the same name as eMPloyees or eMpLOYEES.)

  8. Creating table: Data Types

  9. Creating table: Data Types (Cont.)

  10. Creating table: DEFAULT Specify default value for the column during the INSERT operation Prevents NULL values from entering the columns if the row entered inserted without a value for the column For the DEFAULT : Literal values, expressions, or SQL functions are legal values Another column s name or pseudocolumn are illegal values The DEFAULT value data type must match the column data type

  11. Creating table: Example Crete a Table CREATE TABLE dept (dnum NUMBER(2), dname VARCHAR2(14),dlocation VARCHAR2(13)); Table created Appears when you run the query To Confirm the creation DESCRIBE dept ;

  12. The ALTER TABLE Statement Use the ALTER TABLE statement to: Add a new column Modify an existing column Define a default value for the new column Drop a column

  13. The ALTER TABLE Statement: Syntax Use the ALTER TABLE statement to add, modify or drop columns. ALTER TABLE table ADD (column datatype [DEFAULT expr] [, column datatype]...); ALTER TABLE table MODIFY (column datatype [DEFAULT expr] [, column datatype]...); ALTER TABLE table DROP COLUMN column;

  14. The ALTER TABLE Statement: Syntax

  15. The ALTER TABLE Statement: Example (Add) Use the ADD clause to add columns. ALTER TABLE dept80 ADD (job_id VARCHAR2(9)); Note That You cannot specify where the new column appear The new column becomes the last column directly. If the table already contains rows when the new column added, then the new column is initialized to NULL (as in this example) or default value (if specified) for all the rows

  16. The ALTER TABLE Statement: Example (Add)

  17. The ALTER TABLE Statement: Example (Modify) You can change a column s data type, size, and default value. ALTER TABLE dept80 MODIFY (last_name VARCHAR2(30)); Note That: A change to the default value affects only subsequent insertions to the table You can decrease the width of a column only if the column contains only null values or if the table has no rows. You can change the data type only if the column contains null values.

  18. The ALTER TABLE Statement: Example (DROP) Use the DROP COLUMN clause to drop columns you no longer need from the table. ALTER TABLE dept80 DROP COLUMN job_id; Note that: The column may or may not contain data. Using the ALTER TABLE statement, only one column can be dropped at a time. The table must have at least one column remaining in it after it is altered. Once a column is dropped, it cannot be recovered

  19. Dropping a Table All data and structure in the table is deleted. You cannot roll back the DROP TABLE statement. DROP TABLE dept80;

  20. Truncating a Table The TRUNCATE TABLE statement: Removes all rows from a table Releases the storage space used by that table TRUNCATE TABLE detail_dept; Note That You cannot roll back row removal when using TRUNCATE. Alternatively, you can remove rows by using the DELETE statement. (Later) If the table is the parent of a referential integrity constraint, you cannot truncate the table. Disable the constraint before issuing the TRUNCATE statement.

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#