• Practice for SQL


    1.run on environment

    1. [root@oracle-db-19c ~]#
    2. [root@oracle-db-19c ~]# su - oracle
    3. [oracle@oracle-db-19c ~]$ sqlplus / as sysdba
    4. SQL*Plus: Release 19.0.0.0.0 - Production on Fri Nov 18 20:09:18 2022
    5. Version 19.3.0.0.0
    6. Copyright (c) 1982, 2019, Oracle. All rights reserved.
    7. Connected to:
    8. Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
    9. Version 19.3.0.0.0
    10. SQL> show user
    11. USER is "SYS"
    12. SQL> alter session set container=PDB1;
    13. Session altered.
    14. SQL> show con_name
    15. CON_NAME
    16. ------------------------------
    17. PDB1
    18. SQL> set pagesize 200
    19. SQL> set linesize 200
    20. SQL> conn pstest/pstest@PDB1
    21. Connected.
    22. SQL>
    23. SQL>

    2.Creating Table

    1. SQL>
    2. SQL> create table DEPARTMENTS (
    3. 2 deptno number,
    4. 3 name varchar2(50) not null,
    5. 4 location varchar2(50),
    6. 5 constraint pk_departments primary key (deptno)
    7. 6 );
    8. Table created.
    9. SQL>
    10. SQL> select * from tab;
    11. TNAME TABTYPE CLUSTERID
    12. -------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
    13. DEPARTMENTS TABLE
    14. SQL> create table EMPLOYEES (
    15. 2 empno number,
    16. 3 name varchar2(50) not null,
    17. 4 job varchar2(50),
    18. 5 manager number,
    19. 6 hiredate date,
    20. 7 salary number(7,2),
    21. 8 commission number(7,2),
    22. 9 deptno number,
    23. 10 constraint pk_employees primary key (empno),
    24. 11 constraint fk_employees_deptno foreign key (deptno)
    25. 12 references DEPARTMENTS (deptno)
    26. 13 );
    27. Table created.
    28. SQL>

    Notes: Foreign keys must reference primary keys, so to create a "child" table the "parent" table must have a primary key for the foreign key to reference.

    2.Creating Triggers

    Triggers are procedures that are stored in the database and are implicitly run,When an INSERT, UPDATE, or DELETE occurred on a table or view. Triggers support system and other data events on DATABASE and SCHEMA.

    Triggers are frequently used to automatically populate table primary keys, the trigger examples below show an example trigger to do just this.

    1. SQL>
    2. SQL> create or replace trigger DEPARTMENTS_BIU
    3. 2 before insert or update on DEPARTMENTS
    4. 3 for each row
    5. 4 begin
    6. 5 if inserting and :new.deptno is null then
    7. 6 :new.deptno := to_number(sys_guid(),
    8. 7 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
    9. 8 end if;
    10. 9 end;
    11. 10 /
    12. Trigger created.
    13. SQL> create or replace trigger EMPLOYEES_BIU
    14. 2 before insert or update on EMPLOYEES
    15. 3 for each row
    16. 4 begin
    17. 5 if inserting and :new.empno is null then
    18. 6 :new.empno := to_number(sys_guid(),
    19. 7 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
    20. 8 end if;
    21. 9 end;
    22. 10 /
    23. Trigger created.
    24. SQL>

    3.Inserting Data

    1. SQL> insert into departments (name, location) values('Finance','New York');
    2. 1 row created.
    3. SQL> insert into departments (name, location) values('Development','San Jose');
    4. 1 row created.
    5. SQL>
    6. SQL> column deptno default
    7. SQL> select * from departments;
    8. DEPTNO NAME LOCATION
    9. ---------- -------------------------------------------------- --------------------------------------------------
    10. 3.1602E+38 Finance New York
    11. 3.1602E+38 Development San Jose
    12. SQL>
    13. SQL>
    14. SQL>
    15. SQL> insert into EMPLOYEES
    16. 2 (name, job, salary, deptno)
    17. 3 values
    18. 4 ('Sam Smith','Programmer',
    19. 5 5000,
    20. 6 (select deptno
    21. 7 from departments
    22. 8 where name = 'Development'));
    23. insert into EMPLOYEES
    24. (name, job, salary, deptno)
    25. 1 row created.
    26. SQL> SQL> 2 3 values
    27. 4 ('Mara Martin','Analyst',
    28. 5 6000,
    29. 6 (select deptno
    30. 7 from departments
    31. 8 where name = 'Finance'));
    32. 1 row created.
    33. SQL>
    34. SQL> insert into EMPLOYEES
    35. 2 (name, job, salary, deptno)
    36. 3 values
    37. 4 ('Yun Yates','Analyst',
    38. 5 5500,
    39. 6 (select deptno
    40. 7 from departments
    41. 8 where name = 'Development'));
    42. 1 row created.
    43. SQL>

    4.Indexing Columns

    Typically developers index columns for three major reasons:

    1. To enforce unique values within a column
    2. To improve data access performance
    3. To prevent lock escalation when updating rows of tables that use declarative referential integrity
    1. SQL> column table for a30
    2. SQL> column index for a30
    3. SQL> column column for a30
    4. SQL> column column for a40
    5. SQL> select table_name "Table",
    6. 2 index_name "Index",
    7. 3 column_name "Column",
    8. 4 column_position "Position"
    9. 5 from user_ind_columns
    10. 6 where table_name = 'EMPLOYEES' or
    11. 7 table_name = 'DEPARTMENTS'
    12. 8 order by table_name, column_name, column_position;
    13. Table Index Column Position
    14. ------------------------------ ------------------------------ ---------------------------------------- ----------
    15. DEPARTMENTS PK_DEPARTMENTS DEPTNO 1
    16. EMPLOYEES PK_EMPLOYEES EMPNO 1
    17. SQL>
    18. SQL>
    19. SQL> create index employee_dept_no_fk_idx on employees(deptno);
    20. Index created.
    21. SQL> create unique index employee_ename_idx on employees(name);
    22. Index created.
    23. SQL>

    5. Querying Data

    1. SQL>
    2. SQL> select * from employees;
    3. EMPNO NAME JOB MANAGER HIREDATE SALARY COMMISSION DEPTNO
    4. ---------- -------------------------------------------------- -------------------------------------------------- ---------- --------- ---------- ---------- ----------
    5. 3.1602E+38 Sam Smith Programmer 5000 3.1602E+38
    6. 3.1602E+38 Mara Martin Analyst 6000 3.1602E+38
    7. 3.1602E+38 Yun Yates Analyst 5500 3.1602E+38
    8. SQL>
    9. SQL>
    10. SQL> select e.name employee,
    11. 2 (select name
    12. 3 from departments d
    13. 4 where d.deptno = e.deptno) department,
    14. 5 e.job
    15. 6 from employees e
    16. 7 order by e.name;
    17. EMPLOYEE DEPARTMENT JOB
    18. -------------------------------------------------- -------------------------------------------------- --------------------------------------------------
    19. Mara Martin Finance Analyst
    20. Sam Smith Development Programmer
    21. Yun Yates Development Analyst
    22. SQL>

    6. Adding Columns

    1. SQL> alter table EMPLOYEES
    2. 2 add country_code varchar2(2);
    3. Table altered.
    4. SQL>

    7. Querying the Oracle Data Dictionary

    1. SQL> select table_name, tablespace_name, status
    2. 2 from user_tables
    3. 3 where table_Name = 'EMPLOYEES';
    4. TABLE_NAME TABLESPACE_NAME STATUS
    5. -------------------------------------------------------------------------------------------------------------------------------- ------------------------------ --------
    6. EMPLOYEES USERS VALID
    7. SQL>
    8. SQL> column column_id default
    9. SQL> column column_name for a30
    10. SQL> column data_type for a30
    11. SQL> select column_id, column_name , data_type
    12. 2 from user_tab_columns
    13. 3 where table_Name = 'EMPLOYEES'
    14. 4 order by column_id;
    15. COLUMN_ID COLUMN_NAME DATA_TYPE
    16. ---------- ------------------------------ ------------------------------
    17. 1 EMPNO NUMBER
    18. 2 NAME VARCHAR2
    19. 3 JOB VARCHAR2
    20. 4 MANAGER NUMBER
    21. 5 HIREDATE DATE
    22. 6 SALARY NUMBER
    23. 7 COMMISSION NUMBER
    24. 8 DEPTNO NUMBER
    25. 9 COUNTRY_CODE VARCHAR2
    26. 9 rows selected.
    27. SQL>

    8. Updating Data

    1. SQL> update employees
    2. 2 set country_code = 'US';
    3. 3 rows updated.
    4. SQL> update employees
    5. 2 set commission = 2000
    6. 3 where name = 'Sam Smith';
    7. 1 row updated.
    8. SQL> select name, country_code, salary, commission
    9. 2 from employees
    10. 3 order by name;
    11. NAME CO SALARY COMMISSION
    12. -------------------------------------------------- -- ---------- ----------
    13. Mara Martin US 6000
    14. Sam Smith US 5000 2000
    15. Yun Yates US 5500
    16. SQL>

    9. Aggregate Queries

    1. SQL>
    2. SQL> select
    3. 2 count(*) employee_count,
    4. 3 sum(salary) total_salary,
    5. 4 sum(commission) total_commission,
    6. 5 min(salary + nvl(commission,0)) min_compensation,
    7. 6 max(salary + nvl(commission,0)) max_compensation
    8. 7 from employees;
    9. EMPLOYEE_COUNT TOTAL_SALARY TOTAL_COMMISSION MIN_COMPENSATION MAX_COMPENSATION
    10. -------------- ------------ ---------------- ---------------- ----------------
    11. 3 16500 2000 5500 7000
    12. SQL>

    10. Compressing Data

    As your database grows in size to gigabytes or terabytes and beyond, consider using table compression. Table compression saves disk space and reduces memory use in the buffer cache. Table compression can also speed up query execution during reads. There is, however, a cost in CPU overhead for data loading and DML. Table compression is completely transparent to applications. It is especially useful in online analytical processing (OLAP) systems, where there are lengthy read-only operations, but can also be used in online transaction processing (OLTP) systems.

    You specify table compression with the COMPRESS clause of the CREATE TABLE statement. You can enable compression for an existing table by using this clause in an ALTER TABLE statement. In this case, the only data that is compressed is the data inserted or updated after compression is enabled. Similarly, you can disable table compression for an existing compressed table with the ALTER TABLE...NOCOMPRESS statement. In this case, all data the was already compressed remains compressed, and new data is inserted uncompressed.

    To enable compression for future data use the following syntax.

    1. SQL> alter table EMPLOYEES compress for oltp;
    2. Table altered.
    3. SQL> alter table DEPARTMENTS compress for oltp;
    4. Table altered.

    11. Deleting Data

    1. SQL> delete from employees
    2. 2 where name = 'Sam Smith';
    3. 1 row deleted.
    4. SQL>

    12. Dropping Tables

    1. SQL> drop table departments cascade constraints;
    2. Table dropped.
    3. SQL> drop table employees cascade constraints;
    4. Table dropped.
    5. SQL>

    13. Un-dropping Tables

    If the RECYCLEBIN initialization parameter is set to ON (the default in 10g), then dropping this table will place it in the recycle bin. To see if you can undrop a table run the following data dictionary query:

    1. SQL> select object_name,
    2. 2 original_name,
    3. 3 type,
    4. 4 can_undrop,
    5. 5 can_purge
    6. 6 from recyclebin;
    7. OBJECT_NAME
    8. --------------------------------------------------------------------------------------------------------------------------------
    9. ORIGINAL_NAME TYPE CAN CAN
    10. -------------------------------------------------------------------------------------------------------------------------------- ------------------------- --- ---
    11. BIN$7b6u2Xx2SP/gU4oIqMCr/g==$0
    12. PK_DEPARTMENTS INDEX NO YES
    13. BIN$7b6u2Xx4SP/gU4oIqMCr/g==$0
    14. DEPARTMENTS TABLE YES YES
    15. BIN$7b6u2Xx7SP/gU4oIqMCr/g==$0
    16. EMPLOYEE_ENAME_IDX INDEX NO YES
    17. BIN$7b6u2Xx8SP/gU4oIqMCr/g==$0
    18. EMPLOYEE_DEPT_NO_FK_IDX INDEX NO YES
    19. BIN$7b6u2Xx9SP/gU4oIqMCr/g==$0
    20. PK_EMPLOYEES INDEX NO YES
    21. BIN$7b6u2Xx/SP/gU4oIqMCr/g==$0
    22. EMPLOYEES TABLE YES YES
    23. BIN$7b6u2Xx3SP/gU4oIqMCr/g==$0
    24. DEPARTMENTS_BIU TRIGGER NO NO
    25. BIN$7b6u2Xx+SP/gU4oIqMCr/g==$0
    26. EMPLOYEES_BIU TRIGGER NO NO
    27. 8 rows selected.
    28. SQL>
    29. SQL>
    30. SQL> flashback table DEPARTMENTS to before drop;
    31. flashback table EMPLOYEES to before drop;
    32. select count(*) departments
    33. Flashback complete.
    34. SQL> from departments;
    35. Flashback complete.
    36. SQL> 2
    37. DEPARTMENTS
    38. -----------
    39. 2
    40. SQL> select count(*) employees
    41. 2 from employees;
    42. EMPLOYEES
    43. ----------
    44. 2
    45. SQL>

  • 相关阅读:
    MYSQL多表联查on和where的区别
    Zabbix 使用同一ODBC监控不同版本MySQL
    用于独立系统应用的光伏MPPT铅酸电池充电控制器建模(Simulink实现)
    python爬虫进阶篇(异步)
    wpf devexpress在未束缚模式中生成Tree
    【李宏毅】深度学习-2021HW3-CNN(图像分类)
    屋顶太阳能光伏系统的性能分析指标研究
    单调栈!!!
    Hive insert插入数据与with子查询
    Failed to start The nginx HTTP and reverse proxy server.
  • 原文地址:https://blog.csdn.net/u011868279/article/details/127928927