zoukankan      html  css  js  c++  java
  • 常用SQL语句

    主要总结mysql一些常用知识点

    [常用命令]

    1、查看数据库

    show database;

    2、创建数据库

    create database database_name;

    3、切换数据库

    use database_name;

    4、查看某数据库中所有的数据表

    show table;

    5、创建数据表

    CREATE TABLE my_table (
        name VARCHAR(20),
        owner VARCHAR(20),
        species VARCHAR(20),
        sex CHAR(1), 
        birth DATE, 
        death DATE
    );
    View Code

    6、查看数据表结构

    describe  table_name;  --缩写: desc

    7、查看数据表中的记录

    select  *  from  table_name;
    
    -- 去重复
    select distinct name from table_name

    8、往数据表中添加数据记录

    INSERT INTO table_name
    VALUES('puffball','Diane','hanst','f','1999-03-23',NULL);
    
    -- 指定属性
    insert into user3 (name) value('asfjl');

    9、删除数据

    delete from table_name where name='puffball';

    10、修改数据

    update table_name set name='wang' where owner='haha'

    11、建表约束--主键

    create table user(
        id int primary key,
        name varchar(20)
    ); 
    
     -- 联合主键
    create table user2(
        id int,
        name varchar(20),
        password varchar(20),
        primary key(id,name) 
    );
    
    -- 后来添加主键
    create table user4(
        id int,
        name varchar(20)
    );
    alter table user4 add primary key(id);
    
    -- 删除主键约束
    alter table user4 drop primary key;
    
    -- 修改约束
    alter table user4 modify id int primary key;
    View Code

    12、建表约束--自增

    create table user3(
        id int primary key auto_increment,
        name varchar(20)
    );

    12、建表约束--唯一:约束修饰的字段的值不可以重复

    create table user5(
        id int,
        name varchar(20),
        unique(name)     
    -- 可一起约束多个,不一起重复即可;unique(id,name) 
    );
    
    -- 再另一种写法
    create table user5(
        id int,
        name varchar(20)
    );
    alter table user5 add unique(name);
    
    -- 删除唯一约束
    alter table user5 drop index name
    
    -- modify
    alter table user5 modify name varchar(20) unique;
    View Code

    13、非空约束:修饰的字段不能为NULL

    create table user6(
        id int,
        name varchar(20) not null 
    );
    -- 反null? 异常
    insert into user6 (name) value('jfsl');

    14、默认约束

    create table user7(
        id int,
        name varchar(20),
        age int default 10
    );
    
     insert into user7 (id,name) value(1,'slfj');
     insert into user7 (id,name,age) values(1,'slsfj',5);

    15、外键约束

    create table classes(
        id int primary key,
        name varchar(20)
    );
    create table students(
        id int primary key,
        class_id int,
        foreign key(class_id) references classes(id)
     );

    [查询]

    1、多表查询

    -- 两表查询
    select sname,cno, degree from student,score
    where student.sno = score.sno;
    
    -- 三表查询
    select sname, cname,degree from  student,course,course,score
    where student.sno = score.sno
    and course.cno = score.cno;

    2、分组查询

    -- 子查询加分组求评均
    select cno, avg(degree) from score
    where sno in (select sno from student where class='1233')
    group by cno;
    
    -- year函数与带in关键字的子查询
    select * from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,117));

    3、多层嵌套查询

    -- 
    select tno from teacher where tname='zhang';
    -- 
    select cno from course where tno = (select tno from teacher where tname='zhang');
    
    -- 
    select * from score where cno=()
    --
    select * from score where cno=(select cno from course where tno =(select tno from reacher where tname='zhang'));
    View Code

    4、union与not in

    -- not in
    select prof from teacher where depart='电子工程系';
    select * from teacher where depart='计算机系' and prof not in (select 
    prof from teacher where depart='电子工程系');
    
    select * from teacher where depart='电子工程系' and prof not in (select prof from teacher where depart='计算机系');
    
    -- UNION 操作符用于合并两个或多个 SELECT 语句的结果集
    select * from teacher where depart='计算机系' and prof not in (select prof from teacher where depart='电子工程系')
    union
    select * from teacher where depart='电子工程系' and prof not in (select prof from teacher where depart='计算机系');
    View Code

    5、any与all

    -- any表示至少一个
    select * from score where cno='34'
    and degree>any(select degree from score where cno='334')
    order by degree desc;
    
    -- all表示所有
    select * from score where cno='34'
    and degree>all(select degree from score where cno='334')
    order by degree desc;
  • 相关阅读:
    wget 命令用法详解
    VI编辑器的使用方法
    Android APK反编译就这么简单 详解(附图)
    Nginx与X-Sendfile
    腾讯微博OAuth2.0认证介绍
    haporoxy的keeplaive ZZ
    如何监听非本地IP
    haproxy配置直接重定向url
    LVS与其他负载均衡软件的区别
    J2EE基础总结(4)——JSP
  • 原文地址:https://www.cnblogs.com/lisen10/p/sql.html
Copyright © 2011-2022 走看看