Oracle creates tables, updates, and inserts data

1. Create table in three normal forms
  • First normal form: Each column in the table is required to be an indivisible atomic value, that is, each column cannot be decomposed into other columns. For example, the employee information table consists of information such as name, age, gender, student number, etc. Among them, the name is the smallest indivisible unit, so it can be used as a field in the table.
  • The second paradigm is a step further based on the first paradigm. The second normal form needs to ensure that each column in the database table is related to the primary key, not only to a certain part of the primary key (mainly for joint primary keys). For example, in an employee information table, in addition to the employee's basic information, there is also employee position, salary and other information.
  • Third normal form: Based on the second normal form, each column in the table is required to be directly related to the primary key, rather than indirectly, and there is no transitive dependency. For example, in an employee information table, in addition to the employee's basic information, there is also employee position, salary and other information. At this time, the position and salary columns in the employee information table can be set as foreign keys and directly related to the primary key in the employee basic information table. This can prevent the salary and other information of subordinate employees from being affected due to the resignation of an employee.

--Foreign key refers to one or more fields in one table whose values ​​must exist in a field in another table. Foreign keys are used to establish and strengthen the link between two table data to ensure data integrity and consistency.

Create table

1.CREATE TABLE 表名 
(
字段1, 数据类型1 (长度),
字段2, 数据类型2 (长度),
字段3, 数据类型3 (长度)
....
)

** When the data type is NUMBER, the length can be specified differently, and the default maximum length is 38 bits.
** When the data type is VARCHAR2, the length must be specified, and the maximum length is 4000.
** When the data type is DATE, the length does not need to be specified. 

Insert data

INSERT INTO 表名 values (值1,值2,值3,值4...) 

INSERT INTO 表名 (列名1,列名2,列名3....) VALUES (值1,值2,值3....) -- 可以指定插入列

COMMIT;--Commit transaction
ROLLBACK;--Rollback transaction

--数据的插入
-- 创建空表
CREATE TABLE EMP_TEST7 
AS 
SELECT 
E.EMPNO,
E.ENAME,
E.HIREDATE,
E.HIREDATE AS ETL_DATE
FROM EMP E 
WHERE 1=2;
2.--Modify table
2.1 Syntax structure 1: Add columns
ALTER TABLE 表名 ADD 列名 数据类型
2.2 Syntax structure 2: Modify column type
ALTER TABLE 表名 MODIFY 列名 数据类型 

-- If other data types already exist in this column, it cannot be modified, and empty columns can be modified
-- More for increasing the length

2.3 Syntax structure 3: Modify column name 
ALTER TABLE 列名 RENAME COLUMN 旧列名 TO 新列名 
2.4 Syntax Structure 4: Delete Column
ALTER TABLE 表名 DROP COLUMN 列名
2.5 Syntax structure 5: Modify table name 
ALTER TABLE 表名 RENAME TO 新表名
3. --Delete table
DROP TABLE 表名 -- 删除表结构和数据 

DELETE FROM 表名 -- 删除表中的所有数据

DELETE FROM 表名 WHERE --指定条件进行删除

DELETE FROM EMP_TEST7 WHERE ENAME='SMITH';

-- DELETE deleting data will involve rollback and commit operations.

-- **Truncate table 

TRUNCATE TABLE 表名 -- 摧毁并重建表结构 
 4. -- Modify statement: UPDATE
UPDATE 表名 SET 修改的字段 = 值 -- 修改某字段的所有值

UPDATE 表名 SET 修改的字段 = 值 WHERE 修改数据的条件 -- 按条件修改某个字段的值

UPDATE 表名 SET 修改的字段1=值1 , 修改的字段2=值2 ,.....
WHERE 修改数据的条件 -- 修改多个字段

おすすめ

転載: blog.csdn.net/weixin_57024726/article/details/133383106