zoukankan      html  css  js  c++  java
  • 数据库使用简单总结

    创建一个表
    create table user(
    id number(4),
    password char(4),
    name char(20),
    createTime date
    );
    数字
    number(n) 数字最长几位 如:number(2) 表示最长99
    number(n,m) 浮点数 如:number(7,2) 表示最大数99999.99
    字符串
    char(n) 表示定长字符串(方便查询)
    varchar(n) 表示变长字符串(节省空间)
    varchar2(n) Oracle自己定义的变成字符串
    日期
    date 日期

    插入数据
    方法1 insert into user values(1001,"1234","TOM");
    方法2 insert into user(id,name) values(1002,"Ford");

    分页显示
    set pagesize 100 每100行数据分页显示

    nvl(d1 , d2)方法演示:如果d1为null则用d2替代
    select ename, salary , bonus , salary + nvl(bonus, 0) month_sal from emp_xxx ;

    select empno, ename || job detail from emp_xxx ;
    "||" 符号表示两个数据串接起来 , 类似亍Java中的两个字符串之间的+号


    复制表
    复制表table1为table2
    create table table2 as select * from table1;

    lower() 将字符数据转换为小写 如:select * from table where lower(job) = 'analyst';
    upper() 将数据转换为大写 如:select * from table where upper(job) = 'ANALYST';

    between …and… 关键字
    薪水大亍5000并且小亍10000的员工数据?
    方法1 select * from emp_xxx where salary >= 5000 and salary <= 10000 ;
    方法2 select * from emp_xxx where salary between 5000 and 10000 ;
    薪水不在5000至8000的员工
    方法1 select * from emp_xxx where salary < 5000 and salary >8000;
    方法2 select * from emp_xxx where not betwen 5000 and 8000;

    in( 列表 )
    列出职位是Manager或者Analyst的员工
    方法1 select * from table where job = 'Manager' or job = 'Analyst';
    方法2 select * from table where job in('Manager','Analyst');
    列出职位不是Manager或者Analyst的员工
    方法1 select * from table where job <> 'Manager' or job <> 'Analyst';
    方法2 select * from table where job not in('Manager','Analyst');

    模糊匹配 like %
    1、“% ”表示0到多个字符 , 跟like配合使用
    2、“_”下划线表示一个字符
    select * from emp_xxx where lower(job) like '%sales%' ;
    select * from emp_xxx where job like '_a%' ;

    round() 用于数字的四舍五入
    round(12.34567, 2 ) 保留2位有效数字 12.34
    round(12.34567) 默认0位有效数字 12

    trunc() 用于截取
    trunc(12.34567, 2) 12.34

    count(*) 统计有多少行
    sum(salary) salary列的总和
    avg(nvl(salary,0)) salary列的平均值
    max(salary) salary列的最大值
    min(salary) salary列的最小值

    having子句
    having子句用于对分组后的数据进行过滤。
    注意区别where是对表中数据的过滤 ;having是对分组得到的结果数据进一步过滤

    rename关键字
    rename table1 to table2

    左补齐
    select lpad(ename,10,"*") from table;
    右补齐
    select rpad(ename,10,"*") from table;
    对字段salary字段5000取模
    select salary,mod(salary,5000) from table;

    months_between 两个日期之间的月份数
    add_months 给定一个日期,为该日期增加指定月份
    last_day 找出参数时间点所在月份的最后一天

    将amy的入职日期提前2个月
    select time from emp_xxx a where a.ename = "amy";
    update emp_xxx set time=add_months(tims,-2) where ename="amy";
    select time from emp_xxx a where a.ename = "amy";

    这个月的最后一天是多少号
    select sysdate,last_day(sysdate) from dual;

    转换函数: to_char / to_date / to_number
    to_char to_number
    日期 ---------> 字符 ----------> 数字
    <--------- <---------
    to_date to_char

    to_number用法
    将"$10"乘以10,输出结果
    select to_number("$10","$9,999,999.99")*10 sal from table;
    to_char用法
    将"$10"按指定格式“¥9,999,999.99”输出
    select to_char("$10","$9,999,999.99") from table;

    谁的薪水比张无忌高?
    select name,money from table where money > (select money from table where name = "zhangwuji");

    insert into table(name,age,sex) values('wuzhitong',27,'nan');

    查询谁的薪水比所有叫张无忌的薪水都高?
    select name,salary from table where salary > ALL(select salary from table where name ="zhangwuji");

    哪些人的薪水比仸何一个叫张无忌的员工工资高?
    select name,salary from table where salary > ANY(select salary from table where name ="zhangwuji");

    谁和刘苍松同部门?列出除了刘苍松之外的员工名字
    select name from table where bumen = (select bumen from table where name = "刘苍松") and name <> "刘苍松";

    谁和刘苍松同部门?列出除了刘苍松之外的员工名字( 如果子查询得到的结果是多个 )
    select name from table where bumen in(select bumen from table where name ="刘苍松") and name <> "刘苍松";

    每个部门拿最高薪水的是谁?
    select name,salary,bumen from table where (bumen,salary) in(select bumen,max(salary) from table where bumen is not null group by bumen);

    哪个部门的人数比部门30的人数多?
    select bumen,count(*) from table group by bumen having count(*) > (select count(*) from table where bumen = "30");

    哪个部门的平均薪水比部门20的平均薪水高?
    select bumen,avg(nvl(salary,0)) from table group by bumen having avg(nvl(salary,0)) > (select bumen, avg(nvl(salary,0)) from table where bumen = "20");

    列出员工名字和职位 , 这些员工所在的部门平均薪水大于5000元
    select name,job from table where bumen in(select bumen from table group by bumen having avg(nvl(salary,0)) > 5000);

    结果集操作
    union 和 union all区别
    union去掉重复记录,union all不去重
    union 排序,union all不排序
    intersect 交集
    minus 差集

    合集( union )演示
    select name,salary from table where bumen = 10 union select name,salary from table where salary > 6000;

    表间关联查询

    内连接
    table1 join table2 on 条件(表1叫做驱动表 , 表2叫做匹配表)
    列出员工的姓名和所在部门的名字和城市
    select name,city,location from table1 t1 join table2 t2 on t1.depthno = t2.depthno

    外连接
    左外连接语法结构: table1 left outer join table2 on 条件
    右外连接语法结构: table1 right outer join table2 on 条件
    外连接的特征
    1、如果驱动表在匹配表中找不到匹配记录,则匹配一行空行
    2、外连接的结果集 = 内连接的结果集 + 驱动表在匹配表中匹配不上的记录和空值
    3、外连接的本质是驱动表中的数据一个都不能少
    left outer join 以左表为驱动表
    right outer join 以右表为驱动表

    列出员工的姓名和他所在部门的名字 , 把没有部门的员工也查出来
    select * from table1 t1 left outer join table2 t2 on t1.deptno =t2.deptno
    select * from table2 t2 right outer join table1 t1 on t1.deptno =t2.deptno

    full outer join 全外连接
    1、全外连接可以把两个表中的数据都查出来
    2、全外连接的结果集 = 内连接结果集+驱动表在匹配表中找不到数据的记录和空值+匹配表在驱动表中找不到的数据记录和空值
    3、驱动表和匹配表可以互换
    select * from table1 full outer join table2 on t1.deptno = t2.deptno

    Transaction(事务)
    commit 事务提交 将所有的数据改动提交
    rollback 事务回滚 回退到事务之初

    alter
    add 添加一列
    alter table user add (time char(10));

    rename 修改元素名
    alter table user rename column time to times;

    modify
    alter table user modify(times char(8));

    drop column
    alter table user drop column times

    约束条件
    主键约束=不能重复+不能为null
    方式定义:列级约束和表级约束

    唯一约束(UK unique )

    检查约束(CK,Check)

    外键(FK,Foreign key)

    创建索引
    方法一 使用alter语句
    普通索引:alter table user add index index_name(name);
    唯一索引:alter table user add unique(name);
    PK索引: alter table user add primary key(name);

    方法二 使用create index语句
    普通索引:create index index_name on user(name);
    唯一索引:create unique index index_name on user(name);

    删除索引
    drop index index_name on user;
    alter table user drop index index_name;
    alter table user drop primary key;

    https://www.cnblogs.com/jiangxiaobo/p/6278136.html

  • 相关阅读:
    window计时器函数
    日期函数
    字符的方法
    五大主流浏览器与四大内核
    向一个排好序的数组中插入一个数组。
    冒泡与排序
    数组去重方法1,2,3
    有执行语句:console.log(fn2(2)[3]),补充函数,使执行结果为"hello"
    面试题:一个函数的调用 fn(2)(3)(4) 得到的结果位24; 这个函数是柯里化函数运用了闭包原理
    python中append()与extend()方法的区别
  • 原文地址:https://www.cnblogs.com/wuzhitong/p/8492026.html
Copyright © 2011-2022 走看看