zoukankan      html  css  js  c++  java
  • 简单数据库sql语言

    创建表格:
    create table user(
    id int ( 8 ) primary key unique not null auto_increment,
    p_name char ( 20 ) unique not null,
    p_level int ( 3 ) default 1
    )charset=utf8;
    关键词:primary key (主键),unique (),not null (不为空),auto_increment (自增长),default (默认值).

    删除表:
    drop table hehe;

    增加列:
    alter table lol add lol_type char(10) not null;

    删除列:
    alter table lol drop lol_type;

    添加数据:
    insert into t_user(t_name,t_adr) values ("诸葛亮","lovo2");

    查询所有数据:
    select * from user;

    查询去重:
    select name distinct from user;

    条件查询:
    select * from user where id=1;
    select name from user where id=1;

    多条件查询:
    select name from user where name=? and addr=? ;
    select name from user where name=? or addr=? ;
    select eName from emp where job in("clerk","manager'");

    范围查询:
    select name from lol where s_int not between 1000 and 3000 and sex="男";
    select name from user where id>10 and id<20;
    select * from log_check where data_dt>="2015-04-29" and data_dt<="2015-04-30";

    多表查询:
    select dname from dept where deptNo in (select deptNo from emp group by deptNo having count(*)>(select count(*) from emp where deptNo=20) );

    查询更名:
    select p_name as name,p_addr as addr from user;

    模糊查询:
    select * from t_user where t_name like'%name%';
    select * from t_user where t_name like'%name';
    select * from t_user where t_name like'name_';

    修改数据:
    update t_user set name=?,addr=? where id=?;

    设置外键:
    foreign key ( f_userid ) references t_user( pk_id ) on delete set null;
    foreign key ( f_userid ) references t_user( pk_id ) on delete cascade;

    分组查询:
    select * from emp where sal in ( select max( sal ) from emp group by deptNo ) order by deptNo;

    降序查询:
    select * from emp order by sal desc;

    升序查询:
    select * from emp order by sal;

    最大最小查询:
    select min( hireDate ) as "最早日期" ,max( hireDate ) as "最晚日期" from emp;

    求和查询:
    select sum( sal ) as "员工工资总和" from emp;

    空值查询:
    select eName from emp where comm="" or comm is null or comm=0;

    统计查询:
    select count(dname) from dept where deptNo in(select deptNo from emp);

  • 相关阅读:
    如何在 Linux 中更改 swappiness
    logrotate机制&原理
    Linux命令 – ln 软连接与硬链接区别介绍
    Python实现目录文件的全量和增量备份
    tr -d命令删除与字符无关的符号
    CentOS7搭建时间服务器-chrony
    linux(centos7.0以上版本)安装 mysql-5.7.24-linux-glibc2.12-x86_64.tar 版本的mysql
    运维相关指标数据采集并ES入仓
    Kubernetes容器集群管理环境
    C++调用IDL程序的做法(三)
  • 原文地址:https://www.cnblogs.com/hym881013/p/4541267.html
Copyright © 2011-2022 走看看