Creating tables in an Oracle database is fundamental to efficient data management. This article provides a detailed explanation of table creation syntax and major data types with specific examples, designed to be easily understood by beginners. It also includes practical tips and points to note.
1. Basic Syntax for Table Creation
To create a table in Oracle, use the following basic syntax:
CREATE TABLE table_name (
column_name data_type [constraint],
...
);
- table_name: Give it a unique name that doesn’t conflict with others. Conventionally, plural nouns are often used (e.g.,
employees). - column_name: Assign appropriate names to the data items within the table. Names should be short and easy to understand (e.g.,
emp_id,hire_date). - data_type: Select a type that matches the nature of the data. The choice of type significantly impacts performance and storage efficiency.
- constraint: Set rules to maintain data consistency (e.g.,
NOT NULL,PRIMARY KEY).
Example: Table for Managing Employee Information
CREATE TABLE employees (
emp_id NUMBER(6) PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
gender CHAR(1) CHECK (gender IN ('M', 'F')),
salary NUMBER(8, 2) DEFAULT 0,
hire_date DATE NOT NULL,
profile_picture BLOB,
notes CLOB,
last_update TIMESTAMP DEFAULT SYSTIMESTAMP
);
2. Detailed Explanation of Data Types
Here is a detailed explanation of frequently used data types in Oracle, along with examples.
1. NUMBER
- Purpose: Used to store numerical data.
- Syntax:
NUMBER(precision, scale) - Precision: Maximum number of digits (1 to 38).
- Scale: Number of digits to the right of the decimal point (-84 to 127).
- Example:
salary NUMBER(8, 2);— Max 8 digits, 2 decimal places.
2. CHAR
- Purpose: Stores fixed-length character strings.
- Syntax:
CHAR(size) - Size: Maximum number of characters (1 to 2000).
- Characteristic: If the data is shorter than the specified size, it is padded with extra spaces.
- Example:
gender CHAR(1);— Stores gender.
3. VARCHAR2
- Purpose: Stores variable-length character strings.
- Syntax:
VARCHAR2(size) - Size: Maximum number of characters (1 to 4000).
- Example:
name VARCHAR2(50);— Max 50-character name.
4. BLOB
- Purpose: Stores binary data such as images or videos (Binary Large Object).
- Characteristic: Can store up to 4GB of data.
- Example:
profile_picture BLOB;— Stores a profile picture.
5. CLOB
- Purpose: Stores long-form text data (Character Large Object).
- Characteristic: Can store up to 4GB of character data.
- Example:
notes CLOB;— Stores long notes.
6. DATE
- Purpose: Stores dates and times.
- Characteristic: Supports year, month, day, hour, minute, and second.
- Example:
hire_date DATE;— Stores hire date.
7. TIMESTAMP
- Purpose: Extends the DATE type, recording up to nanosecond precision.
- Example:
last_update TIMESTAMP(3);— Stores time with 3 fractional seconds.
3. Practical Example: Creating a Product Management Table
Below is an example of a product management table utilizing various data types.
CREATE TABLE product_catalog (
product_id NUMBER(10) PRIMARY KEY,
product_name VARCHAR2(100) NOT NULL,
description CLOB,
image BLOB,
price NUMBER(10, 2) DEFAULT 0,
release_date DATE,
last_updated TIMESTAMP DEFAULT SYSTIMESTAMP
);
Data Insertion Example
INSERT INTO product_catalog (product_id, product_name, price, release_date)
VALUES (1, 'Smartphone X', 799.99, TO_DATE('2024-12-15', 'YYYY-MM-DD'));
4. Design Best Practices
- Choose Data Types Carefully Select types based on the nature of the data, such as
NUMBERfor numbers andVARCHAR2for variable-length strings. - Utilize Constraints Ensure data integrity by setting
PRIMARY KEY,FOREIGN KEY, etc. - Readable Naming Use a consistent naming convention for columns and tables (e.g., snake_case or camelCase).
- Consider Storage Efficiency Avoid setting data types larger than necessary.
5. Common Errors and Solutions
- Error 1: ORA-00955: name is already used by an existing object
- Cause: A table with the same name already exists.
- Solution: Drop the table and recreate it.
DROP TABLE table_name CASCADE CONSTRAINTS; - Error 2: ORA-01747: invalid user.table.column, table.column, or column specification
- Cause: Using a reserved word or special character for a column name.
- Solution: Change the column name or enclose it in double quotes.
"COLUMN"
Through this article, you should now have a solid understanding of the basics of table creation in the Oracle database. As a next step, it is recommended to learn about index creation and performance tuning.
[reference]
Oracle Database SQL Language Reference, 19c


コメント