zoukankan      html  css  js  c++  java
  • mysql 基础操作

    1. 添加删除数据库
    查看当前有哪些DB:show databases;
    添加DB:create database gc;
    删除DB:drop database gc;
     
    2. 增删table
    创建数据库表table
    create table table_name(
    colum_name data_type,
    colum_name data_type,
    .
    .
    .
    colum_name data_type
    );
    删除数据库表table
    drop table table_name;
     
    创建表前要先选定一个数据库
    use 数据库名; 选定数据库
    show tables; 查看有哪些数据库表
    describe 表名; 查看表结构
    drop table 表名; 删除表
     
    3. 修改table - 增删列
    增加列
    alter table 表名 add 列名 数据类型 [not null] [default];
    例子:
    alter table account add c1 int(11) not null default 1;
    删除列
    alter table 表名 drop 列名;
    例子:
    alter table account drop c1;
     
    4. 修改table - 修改列信息和表名
    修改列信息
    alter table 表名 change 旧列名 新列名 数据类型;
    1) 只改列名:
    数据类型和原来一样,旧列名不等于新列名
    2)只改数据类型:
    新列名等于旧列名,数据类型改变
    3)列名和数据类型都改变
    修改表名
    alter table 表名 rename 新表名;
    例子:
    alter table account rename newaccount;
     
    5. 插入和查看表数据
    查看表数据
    select * from 表名;
    select 列名1,列名2,.... from 表名;
    插入数据
    insert into 表名 values(值1,值2,.....); 注:如果有三列,后面的值必须是三个。
    insert into 表名 (列1,列2.....) values(值1,值2,.....); 给指定列插入值
     
    6. where 条件
    1)select * from 表名 where 列名 运算符 值;
    列子:
    select * from book where title = 't';
     
    在mysql中 等于 是一个 =
     
    between 在两个值范围内,like按某个模式查找
    2)组合条件 and or
    where后面可以通过and与or运算符组合多个条件赛选
    语法:
    select * from 表名 where 列1 = xxx and 列2 = xx or 列3 > xx;
     
    7. where的null判断
    null的判断 - is/is not
    select * from 表名 where 列名 is null;
    select * from 表名 where 列名 is not null;
     
    8. select distinct
    distinct(精确的)去重,仅保留完全不同的查询结果
    select distinct 列名 from 表名;
    例子:
    select distinct title from book;
    select distinct title,content from book;
     
    9. select结果按order by排序
    1)按单一列名排序:
    select * from 表名 [where 子句] order by 列名 [asc/desc];
    2)按多列排序:
    select * from 表名 [where 子句] order by 列1 [asc/desc], 列2 [asc/desc];
    注:不加asc或者desc,默认为asc(升序)
    例子:select * from book order by name asc;
    select * from book order by id desc;
     
     
    10. select 结果按limit截取
    select * from 表名 [where 子句] [order by 子句] limit [offset,] rowCount;
    例子:
    select * from book order by limit 2,4;
     
    offset:查询结果的起始位置,第一条记录的其实是0
    rowCount:从offset位置开始,获取的记录条数
    注:limit rowCount = limit 0,rowCount
     
    11. insert into 与select组合
    一般用法:
    insert into [表名] values(值1,值2,....);
    insert into [表名] (列1,列2,...) values(值1,值2,....);
     
    insert into 与select组合用法:
    insert into [表名1] select 列1,列2 from [表名2];
    insert into [表名1](列1,列2) select 列3,列4 from [表名2];
     
    例子:insert into book2 select * from book where id != 1;
    insert into book2(title) select content from book;
     
    12. 更新表数据
    update 语法
    修改单列:
    update 表名 set 列名 = xxx [where 子句];
    修改多列:
    update 表名 set 列名1 = xxx,列名2 = xxx ...[where 子句];
    例子:
    update book set content = 'nice day' where id = 3;
    update book set content = 'bad day',title = 'nice tree' where id = 3;
     
    13. where的in操作符
    select * from 表名 where 列名 in (value1,value2....);
    select * from 表名 where 列名 in (select 列名 from 表名);
     
    例子:
    select * from book where title in ('sun','color');
    select * from book where title in (select from book2 title where id < 4);
     
    注:列名 in (value1,value2 ...)等同于 列名 = value1 or 列名 = value2 ....
     
    14. between操作符
    select * from 表名 where 列名 between 值1 and 值2;
    select * from 表名 where 列名 not between 值1 and 值2;
     
    例子:
    select * from book where id between 1 and 3;
    select * from book where id not between 3 and 5;
     
    15. where 的like操作符(字符串的模糊匹配)
    select * from 表名 where 列名 [not] like pattern;
     
    pattern:匹配模式,比如'abc' '%abc' 'abc%' '%abc%'
    "%" 是一个通配符,理解上可以把它当做任何字符串
    例子:
    '%abc' 能匹配 'erttsabc'
    select * from book where id like '1';
    select * from book where title like 't%';
    select * from book where title like '%day'
    select * from book where tiltle like '%color%';
     
  • 相关阅读:
    hive.exec.parallel参数
    MySQL FEDERATED 提示
    mapreduce作业单元测试
    linux 更改mysql的数据库目录
    SQL Server 2008数据库邮件配置及应用
    mysql主键大小写不敏感的解决办法
    java遍历hashMap、hashSet、Hashtable
    Linux下命令行显示当前全路径方法
    通过SQL Server操作MySQL的步骤和方法
    Linux shell获取时间和时间间隔(ms级别)
  • 原文地址:https://www.cnblogs.com/xiaochaoyxc/p/6204336.html
Copyright © 2011-2022 走看看