选读SQL经典实例笔记03_DML和元数据

news2024/9/21 16:30:48

 

1. 复制数据到另一个表

1.1.  sql

 insert into dept_east (deptno,dname,loc)
 select deptno,dname,loc
   from dept
  where loc in ( 'NEW YORK','BOSTON' )

2. 复制表定义

2.1. 复制表结构,而不复制数据

2.2. MySQL

2.3. PostgreSQL

2.4. Oracle

2.5. sql

create table dept_2
 as
 select *
   from dept
  where 1 = 0

2.6. SQL Server

2.6.1.  sql

select *
   into dept_2
   from dept
  where 1 = 0

2.7. DB2

2.7.1. create table dept_2 like dept

3. 删除违反参照完整性的记录

3.1. 从表里删除一些记录,因为在另一个表里不存在与这些记录相匹配的数据

3.2. 删除其实就是查询,最重要的步骤是要写出正确的WHERE子句条件,以找出要删除哪些记录

3.3. sql

delete from emp
 where not exists (
   select * from dept
    where dept.deptno = emp.deptno
)

4. 删除重复记录

4.1. sql

create table dupes (id integer, name varchar(10))
insert into dupes values (1, 'NAPOLEON')
insert into dupes values (2, 'DYNAMITE')
insert into dupes values (3, 'DYNAMITE')
insert into dupes values (4, 'SHE SELLS')
insert into dupes values (5, 'SEA SHELLS')
insert into dupes values (6, 'SEA SHELLS')
insert into dupes values (7, 'SEA SHELLS')
select * from dupes order by 1

4.2. 如果要删除重复记录,首先要明确两行数据在什么条件下才会被认为是“重复的记录”

4.2.1. sql

select min(id)
  from dupes
 group by name

4.2.2.   sql

delete from dupes
   where id not in ( select min(id)
                       from dupes
                      group by name )

5. 删除被其他表参照的记录

5.1. sql

create table dept_accidents
( deptno         integer,
  accident_name varchar(20) )
insert into dept_accidents values (10,'BROKEN FOOT')
insert into dept_accidents values (10,'FLESH WOUND')
insert into dept_accidents values (20,'FIRE')
insert into dept_accidents values (20,'FIRE')
insert into dept_accidents values (20,'FLOOD')
insert into dept_accidents values (30,'BRUISED GLUTE')
select * from dept_accidents

5.2. 识别哪些部门发生过3次以上事故

5.2.1. sql

select deptno
  from dept_accidents
 group by deptno
having count(*) >= 3

5.3. 删除在上述部门工作的员工

5.3.1.  sql

delete from emp
  where deptno in ( select deptno
                      from dept_accidents
                     group by deptno
                    having count(*) >= 3 )

6. 元数据查询

6.1. SMEAGOL 模式

7. 列举模式中的表

7.1. MySQL

7.2. PostgreSQL

7.3. SQL Server

7.4. 信息模式

7.4.1. information schema,这是按照ISO SQL 标准定义的一组视图

7.4.2.  sql

select table_name
   from information_schema.tables
  where table_schema = 'SMEAGOL'

7.5. Oracle

7.5.1. sql

select table_name
  from all_tables
 where owner = 'SMEAGOL'

7.6. DB2

7.6.1.  sql

select tabname
   from syscat.tables
  where tabschema = 'SMEAGOL'

8. 列举字段

8.1. MySQL

8.2. PostgreSQL

8.3. SQL Server

8.4. 信息模式

8.4.1.  sql

select column_name, data_type, ordinal_position
   from information_schema.columns
  where table_schema = 'SMEAGOL'
    and table_name   = 'EMP'

8.5. Oracle

8.5.1.   sql

select column_name, data_type, column_id
    from all_tab_columns
   where owner      = 'SMEAGOL'
     and table_name = 'EMP'

8.6. DB2

8.6.1.  sql

select colname, typename, colno
   from syscat.columns
  where tabname   = 'EMP'
    and tabschema = 'SMEAGOL'

9. 列举索引列

9.1. MySQL

9.1.1. show index from emp

9.2. PostgreSQL

9.2.1.   sql

select a.tablename,a.indexname,b.column_name
    from pg_catalog.pg_indexes a,
         information_schema.columns b
   where a.schemaname = 'SMEAGOL'
     and a.tablename  = b.table_name

9.3. Oracle

9.3.1. sql

select table_name, index_name, column_name, column_position
  from sys.all_ind_columns
 where table_name  = 'EMP'
   and table_owner = 'SMEAGOL'

9.4. SQL Server

9.4.1.   sql

select a.name table_name,
         b.name index_name,
          d.name column_name,
          c.index_column_id
     from sys.tables a,
          sys.indexes b,
          sys.index_columns c,
          sys.columns d.
   where a.object_id = b.object_id
    and b.object_id = c.object_id
    and b.index_id  = c.index_id
    and c.object_id = d.object_id
   and c.column_id = d.column_id
    and a.name      = 'EMP'

9.5. DB2

9.5.1.   sql

select a.tabname, b.indname, b.colname, b.colseq
    from syscat.indexes a,
         syscat.indexcoluse b
   where a.tabname   = 'EMP'
     and a.tabschema = 'SMEAGOL'
     and a.indschema = b.indschema
     and a.indname   = b.indname

10. 列举约束

10.1. MySQL

10.2. PostgreSQL

10.3. SQL Server

10.4. 信息模式

10.4.1.   sql

select a.table_name,
          a.constraint_name,
          b.column_name,
          a.constraint_type
     from information_schema.table_constraints a,
          information_schema.key_column_usage b
    where a.table_name      = 'EMP'
      and a.table_schem     = 'SMEAGOL'
      and a.table_name      = b.table_name
     and a.table_schema    = b.table_schema
     and a.constraint_name = b.constraint_name

10.5. Oracle

10.5.1.   sql

select a.table_name,
          a.constraint_name,
         b.column_name,
          a.constraint_type
     from all_constraints a,
          all_cons_columns b
    where a.table_name      = 'EMP'
      and a.owner           = 'SMEAGOL'
      and a.table_name      = b.table_name
     and a.owner           = b.owner
     and a.constraint_name = b.constraint_name

10.6. DB2

10.6.1.   sql

select a.tabname, a.constname, b.colname, a.type
    from syscat.tabconst a,
         syscat.columns b
   where a.tabname   = 'EMP'
     and a.tabschema = 'SMEAGOL'
     and a.tabname   = b.tabname
     and a.tabschema = b.tabschema

11. 列举非索引外键

11.1. 如果通过外键实现父子关系,那么为子表里对应的列加上索引有助于减少锁

11.2. 子表和父表常用外键列做连接查询,因而加上索引有助于提升查询性能

11.3. MySQL

11.3.1. 针对特定的表执行SHOW INDEX命令,并比较其输出结果与INFORMATION_SCHEMA.KEY_COLUMN_USAGE.COLUMN_NAME的异同

11.3.2. 如果KEY_COLUMN_USAGE里有对应的COLUMN_NAME,但是SHOW INDEX输出的结果里却没有,那么就说明该列没有索引

11.4. PostgreSQL

11.4.1.   sql

select fkeys.table_name,
          fkeys.constraint_name,
          fkeys.column_name,
          ind_cols.indexname
     from (
   select a.constraint_schema,
          a.table_name,
          a.constraint_name,
          a.column_name
    from information_schema.key_column_usage a,
         information_schema.referential_constraints b
   where a.constraint_name    = b.constraint_name
     and a.constraint_schema  = b.constraint_schema
     and a.constraint_schema  = 'SMEAGOL'
     and a.table_name         = 'EMP'
         ) fkeys
         left join
         (
  select a.schemaname, a.tablename, a.indexname, b.column_name
    from pg_catalog.pg_indexes a,
         information_schema.columns b
   where a.tablename  = b.table_name
     and a.schemaname = b.table_schema
         ) ind_cols
      on (    fkeys.constraint_schema = ind_cols.schemaname
          and fkeys.table_name        = ind_cols.tablename
          and fkeys.column_name       = ind_cols.column_name )
   where ind_cols.indexname is null

11.5. Oracle

11.5.1.   sql

select a.table_name,
          a.constraint_name,
          a.column_name,
          c.index_name
     from all_cons_columns a,
          all_constraints b,
          all_ind_columns c
    where a.table_name       = 'EMP'
      and a.owner            = 'SMEAGOL'
     and b.constraint_type  = 'R'
     and a.owner            = b.owner
     and a.table_name       = b.table_name
     and a.constraint_name  = b.constraint_name
     and a.owner            = c.table_owner  (+)
     and a.table_name       = c.table_name   (+)
   and a.column_name      = c.column_name  (+)
     and c.index_name       is null

11.6. SQL Server

11.6.1.   sql

select fkeys.table_name,
          fkeys.constraint_name,
          fkeys.column_name,
          ind_cols.index_name
     from (
   select a.object_id,
          d.column_id,
          a.name table_name,
          b.name constraint_name,
         d.name column_name
    from sys.tables a
         join
         sys.foreign_keys b
      on (   a.name      = 'EMP'
         and a.object_id = b.parent_object_id
         )
         join
         sys.foreign_key_columns c
         on ( b.object_id = c.constraint_object_id )
            join
            sys.columns d
         on (   c.constraint_column_id = d.column_id
         and a.object_id               = d.object_id
         )
         ) fkeys
         left join
         (
  select a.name index_name,
         b.object_id,
         b.column_id
    from sys.indexes a,
         sys.index_columns b
   where a.index_id = b.index_id
          ) ind_cols
      on (     fkeys.object_id = ind_cols.object_id
          and fkeys.column_id = ind_cols.column_id )
   where ind_cols.index_name is null

11.7. DB2

11.7.1.   sql

select fkeys.tabname,
          fkeys.constname,
          fkeys.colname,
          ind_cols.indname
     from (
   select a.tabschema, a.tabname, a.constname, b.colname
     from syscat.tabconst a,
          syscat.keycoluse b
    where a.tabname   = 'EMP'
     and a.tabschema = 'SMEAGOL'
     and a.type      = 'F'
     and a.tabname   = b.tabname     and a.tabschema = b.tabschema
         ) fkeys
         left join
         (
  select a.tabschema,
         a.tabname,
         a.indname,
         b.colname
    from syscat.indexes a,
         syscat.indexcoluse b
   where a.indschema = b.indschema
     and a.indname   = b.indname
         ) ind_cols
      on (     fkeys.tabschema = ind_cols.tabschema
           and fkeys.tabname   = ind_cols.tabname
          and fkeys.colname   = ind_cols.colname )
   where ind_cols.indname is null

12. 用SQL生成SQL

12.1. 使用字符串拼接SQL 语句,通过查询某些表来获取需要填入的数据(例如数据库对象名称)

12.2. Oracle示例

12.2.1. /* 生成SQL以计算各个表的行数 */

select 'select count(*) from '||table_name||';' cnts
  from user_tables;
CNTS
--------------------------------------
select count(*) from ANT;
select count(*) from BONUS;
select count(*) from DEMO1;
select count(*) from DEMO2;
select count(*) from DEPT;
select count(*) from DUMMY;

12.2.2.  /* 禁用所有表的外键约束 */

select 'alter table '||table_name||
        ' disable constraint '||constraint_name||';' cons
   from user_constraints
  where constraint_type = 'R';
CONS
--------------------------------------------------------
alter table ANT disable constraint ANT_FK;
alter table BONUS disable constraint BONUS_FK;
alter table DEMO1 disable constraint DEMO1_FK;
alter table DEMO2 disable constraint DEMO2_FK;
alter table DEPT disable constraint DEPT_FK;
alter table DUMMY disable constraint DUMMY_FK;

12.2.3.  /* 根据EMP表的某些列生成插入脚本 */

select 'insert into emp(empno,ename,hiredate) '||chr(10)||
       'values( '||empno||','||''''||ename
       ||''',to_date('||''''||hiredate||''') );' inserts
 from emp
where deptno = 10;
INSERTS
---------------------------------------------------------------
insert into emp(empno,ename,hiredate)
values( 7782,'CLARK',to_date('09-JUN-1981 00:00:00') );
insert into emp(empno,ename,hiredate)
values( 7839,'KING',to_date('17-NOV-1981 00:00:00') );
insert into emp(empno,ename,hiredate)
values( 7934,'MILLER',to_date('23-JAN-1982 00:00:00') );

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/733276.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

递归--Fibonacci数列 I

描述 众所周知,Fibonacci数列是一个著名数列。它的定义是: 本组题目共有 5 题,请分别用 5 种不同的方式来完成,并比较这些做法的时间。 本题要求采用第一种方法:递归,且不得使用数组记忆结果。 输入描述 …

备忘录方法--Fibonacci数列 IV

描述 众所周知,Fibonacci数列是一个著名数列。它的定义是: 本题要求采用第四种方法:备忘录方法,即记忆化搜索。 具体做法是:用数组把曾经求出来的 Fibonacci 数列保存下来,以后要的时候直接取出来。 输入…

【剑指offer】5.重建二叉树(java)

文章目录 重建二叉树描述示例1示例2示例3思路完整代码 重建二叉树 描述 给定节点数为 n 的二叉树的前序遍历和中序遍历结果,请重建出该二叉树并返回它的头结点。 例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建出如下…

vue+electron项目实战总结(遇到了哪些问题,是如何解决的,哪个阶段需要做什么,附带一些常用方法)

electron作为一个将网页打包成桌面应用的工具 非常强大,在使用electron的时候 要相信 它可以实现所有现代软件能够支撑的功能,下面我总结一下我在 vueelectron经过4次 大版本更新才趋于稳定的开发经验。 一、开发套路: 消息通信数据驱动 使用…

LIN总线与RS485总线

LIN(Local Interconnect Network,局部互连网络)总线和RS485都是用于设备间通信的串行通信协议。下面我将分别列出它们的优势和劣势。 LIN总线的优势: 简单性:LIN总线的硬件和协议简单,易于实现和维护。成…

多元回归预测 | Matlab基于逻辑回归(Logistic Regression)的数据回归预测,多输入单输出模型

文章目录 效果一览文章概述部分源码参考资料效果一览 文章概述 多元回归预测 | Matlab基于逻辑回归(Logistic Regression)的数据回归预测,多输入单输出模型 评价指标包括:MAE、RMSE和R2等,代码质量极高,方便学习和替换数据。要求2018版本及以上。 部分源码 %% 清空环境变量…

炸裂了…京东一面索命40问,过了就50W+

说在前面 在40岁老架构师尼恩的(50)读者社区中,经常有小伙伴,需要面试京东、阿里、 百度、头条、美团等大厂。 下面是一个小伙伴成功拿到通过了京东一次技术面试,最终,小伙伴通过后几面技术拷问、灵魂拷问…

MySQL的存储引擎与基本使用

目录 一、前言 1.MySQL的介绍 二、存储引擎 1.什么是存储引擎 2.常见存储引擎 2.1.InnoDB(MySQL默认引擎) 2.1.1.四种隔离级别 2.2.MyISAM存储引擎 2.3.Memory存储引擎 3.ACID事务 三、CRUD操作 1.插入数据 2.查询数据 3.修改数据 4.删除数据 四、数据库 1.默认…

C#学习之路-基本语法

C# 是一种面向对象的编程语言。在面向对象的程序设计方法中,程序由各种相互交互的对象组成。相同种类的对象通常具有相同的类型,或者说,是在相同的 class 中。 using System; using System.Collections.Generic; using System.Linq; using S…

OpenCV库进行图像旋转、仿射变换和透视变换

#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp>

解决Linux操作系统ping不同www.baidu.com问题

首先给自己管理员权限 输入代码 su然后输入密码密码不会显示打完直接回车 输入下面代码配置网络 cd /etc/sysconfig/network-scripts 输入以下代码进入ens33管理 vim ifcfg-ens33 输入 i i用方向键把光标移到 ONBOOT NO 把 no 改为 yes 按下 ESC 退出编辑模式 直接敲…

二叉树前中后序的非递归实现

前言 &#xff1a; 递归我们会有一些问题的 为什么有递归就一定有非递归呢&#xff1f;&#xff1f;首先递归是有一定缺陷的 递归真正的缺陷是&#xff0c;每一个程序运行起来呢都是一个线程的形式&#xff0c;但是每一个线程都会有独立的栈空间&#xff0c;但是栈空间是很…

Spring Boot 中的 Future 接口是什么,如何使用

Spring Boot 中的 Future 接口是什么&#xff0c;如何使用 在异步编程中&#xff0c;我们通常需要处理一些耗时的操作。一种常见的做法是使用 Future 接口来代表一个异步操作的结果。在 Spring Boot 中&#xff0c;Future 接口被广泛应用于异步编程中&#xff0c;本文将介绍 S…

【C++】VSCode 使用 C/C++ Compile Run 插件时,设置默认运行的编译参数的方法

为什么要设置参数 最近在学习C&#xff0c;在学习多线程的时候&#xff0c;发现使用C11中的thread类写的代码编译会报错&#xff1a; * 正在执行任务: g -Wall -Wextra -g3 /Users/anweiyang/studySrc/C/ThreadTest.cpp -o /Users/anweiyang/studySrc/C/output/ThreadTest /U…

OpenCV使用putText将文字绘制到图像上

#include <opencv2/opencv.hpp>int main(int argc, char **argv) {cv::Mat image = cv::imread(

开发实例:实现一个时间轮算法

时间轮算法是一种比较常见的时间计数器算法&#xff0c;它主要用于定时器的处理。在Java开发中&#xff0c;我们可以使用这种算法来实现非常高效且精准的定时器功能。下面&#xff0c;我将为大家介绍一个基于时间轮算法的定时器实现方法。 1、定义时间轮的数据结构 首先&…

spring 详解一 IOC(BeanFactory和ApplicationContext)

spring概述 重要部分 Spring是一个容器&#xff0c;用来管理java对象的创建以及其他功能的扩展&#xff0c;目前java的生态已经离不开spring&#xff0c;所以spring在java领域是一个极其重要的框架&#xff0c;在spring的思想中IOC(控制反转&#xff09;和AOP(切面编程)是重要…

Portraiture最新PS/LR 4.1.0.3皮肤修饰插件

Portraiture是一款惹人喜爱的PS磨皮插件。它能智能地对图像中的皮肤材质、头发、眉毛、睫毛等部位进行平滑和减少疵点处理&#xff0c;相对于Camera RAW&#xff0c;它能选择肌肤的色彩范围&#xff0c;对选择的部分进行单独处理。这样避免了其他部分同时被美化。 Portraiture…

C#(五十)之stringBuilder类

使用StringBuilder 需引用命名空间 using System.Text; String类与StringBuilder类的区别&#xff1a; string是各位用的最多的类型之一&#xff0c;是一个特殊的引用类型&#xff0c;直接派生于Object&#xff0c;因此它的值储存在托管堆上。构造一个新字符串的时候&#xf…

使用TestNG搭建自动化测试框架设计说明书

TestNG自动化测试框架V1.0 1. 背景............................................................................................................................ 4 1.1 编写背景.....................................................................................…