From Getting Started to Mastering: A Complete Guide to the Oracle Database

How to create tables and insert data in Oracle database

Creating tables and inserting data are very common tasks in an Oracle database. This article will guide you how to use Oracle SQL statements to create tables and insert data, and provide some practical code examples.

Environmental preparation

First, to make sure you have installed and properly configured the Oracle database, open a session in a database client such as SQL*Plus or SQL Developer.

create table

To create a new table, you can use the CREATE TABLE statement. The CREATE TABLE statement allows you to define the structure of the table and information such as the name, data type, and constraints of each column. Here is an example of creating an "employees" table:

CREATE TABLE employees (
  employee_id   NUMBER(5) PRIMARY KEY,
  first_name    VARCHAR2(50),
  last_name     VARCHAR2(50),
  hire_date     DATE,
  salary        NUMBER(10,2)
);

In the above example, we defined a table named "employees" with 5 columns: "employee_id", "first_name", "last_name", "hire_date" and "salary". Among them, "employee_id" is defined as the primary key column, specified by the PRIMARY KEY keyword.
You can also add other constraints in the CREATE TABLE statement, such as UNIQUE constraints, NOT NULL constraints, etc., to ensure data integrity and validity.

insert data

After creating a table, you can use the INSERT INTO statement to insert data into the table. The INSERT INTO statement allows you to specify the columns and corresponding values ​​to be inserted. Here is an example of inserting a record into the "employees" table:

INSERT INTO employees (employee_id, first_name, last_name, hire_date, salary)
VALUES (1, 'John', 'Doe', TO_DATE('2022-01-01', 'YYYY-MM-DD'), 5000);

In the above example, we have inserted a record into the "employees" table, which contains the values ​​of 5 columns such as "employee_id", "first_name", "last_name", "hire_date" and "salary".
If you want to insert multiple records at once, you can use the INSERT INTO SELECT statement. The INSERT INTO SELECT statement allows you to select data from other tables or query result sets and insert it into the target table. Here is an example of inserting multiple records:

INSERT INTO employees (employee_id, first_name, last_name, hire_date, salary)
SELECT 2, 'Jane', 'Smith', TO_DATE('2022-02-01', 'YYYY-MM-DD'), 6000 FROM DUAL UNION ALL
SELECT 3, 'Mike', 'Johnson', TO_DATE('2022-03-01', 'YYYY-MM-DD'), 7000 FROM DUAL;

In the above example, we have selected two records from the "DUAL" pseudo-table using the INSERT INTO SELECT statement and inserted them into the "employees" table.

Query data

After inserting data, you can use the SELECT statement to query the data in the table. The SELECT statement allows you to specify columns to retrieve and conditions to filter and sort data. Here is an example query for all records in the "employees" table:

SELECT * FROM employees;

In the above example, we used the SELECT * statement to retrieve all the columns and records in the "employees" table.
You can also add filters using the WHERE clause, specify collations using the ORDER BY clause, and use other aggregate functions and operators to perform more complex query operations.

change the data

In addition to inserting data, you can also use the UPDATE statement to modify data in a table. The UPDATE statement allows you to update specified columns and corresponding values, optionally using a WHERE clause for conditional filtering. Here's an example that updates an employee's salary in the "employees" table:

UPDATE employees SET salary = 5500 WHERE employee_id = 1;

In the above example, we use the UPDATE statement to update the salary of the employee whose "employee_id" is 1 in the "employees" table to 5500.

delete data

If you need to delete the data in the table, you can use the DELETE statement. The DELETE statement allows you to delete records that meet specified criteria. Here's an example that deletes an employee from the "employees" table:

DELETE FROM employees WHERE employee_id = 2;

In the above example, we have deleted the employee whose "employee_id" is 2 from the "employees" table using the DELETE statement.

in conclusion

Through this article, you have learned how to create tables, insert data, query data, modify data and delete data in the Oracle database. By using Oracle SQL statements, you can easily manage tables and data in the database.
Please adjust and expand the code samples appropriately according to your own needs and actual situation. Hope this blog can help you when using Oracle database!
If you are interested in other Oracle database operations, such as query, join table, transaction processing, etc., you can continue to learn and explore more functions and syntax of Oracle SQL.

Guess you like

Origin blog.csdn.net/weixin_46254812/article/details/131265669