zoukankan      html  css  js  c++  java
  • MySQL数据库语句

       1064表示语法错误
    create database  dsay00; 创建数据库
    create database  day00 character set urf8 ; 创建数据库指定字符集urf8
    create database day00 character set  utf8 collate utf8_bin;创建数据库指定字符集和校验规则
    show databases;显示所有的数据库
    show database day00;显示数据库的信息
    show character set;显示MySQL支持的字符集gbk urf8;
    show create database  day00 ;显示创建数据库的字符集
    show collation like 'utf8%';显示指定字符集下的校验规则

    use day00 ;进入/切换对应的数据库

    drop database day00;删除数据库
    alter database day00 character set 'gbk';修改数据库的字符集
    alter database day00 collate gbk_bin;修改校验规则
    select database();获取当前正在使用的数据库

    show tables ; 显示所有的表
    show create table stu;查看创建的表
    desc stu;显示一个表的详细信息

    创建表
    create table 数据表名(
     列名 列类型[约束],
     列名 列类型,
     列名 列类型
    );
    create table stu(
     name varchar(大小),
     age int,
     num int,
     birthday date
     
    );

    MySQL的数据类型

     数值类型
     tinyint
     smallint
     MEDIUMINT
     int (最常用)
     bigint


     时间和日期类型
     列类型
      “零”值
     DATETIME
      '0000-00-00 00:00:00'
     DATE
      '0000-00-00'
     TIMESTAMP   (时间戳)
      00000000000000
     TIME
      '00:00:00'
      时间和时间戳的区别:
      date:只包含日期,如果传入数据时,没有指定值,将是null
      timestamp:包含日期和时间,如果传入的数据时,没有指定值,将是当前的系统时间
     
     字符串类型
      char和varchar型
      CHAR列的长度固定为创建表时声明的长度。长度可以为从0到255的任何值
      VARCHAR列中的值为可变长字符串。长度可以指定为0到65,535之间的值
      char(character)定长,例如指定长度100,实际传入值为20,其余字符用空格补全
      varchar(variable character)可变长度,例如:指定长度100,实际传入值20.实际存储就是20个字符
     
      create table stu2(
      name char(20),
      age int,
      birthday date,
      login timestamp,
      time time
      
      );
     insert into stu2 values('tom',10,'1998-10-10',null,'12:30:50');//插入数据
     select * from stu;//查询数据

     约束
     not null 非空约束
     unique  唯一 (但是null值不等于null值)
     unique not null  唯一非空
     primary key 主键约束 (非空唯一) auto_increment 自动增长
     
     create table employee(
     id int primary key  auto_increment,
     name varchar(10) not null,
     phone_num varchar(15) unique
     );
     对表操作
     删表
     drop table temp;
     修改表
     添加列
     alter table  employee2
     add hobby  varchar(20);
     删除列
     alter table  employee2
     drop hobby;
     修改列的长度
     alter table employee2
     modify name varchar(20);
     修改列的数据类型
     alter table employee2
     modify name int;   
     修改列的名字
     alter table  employee2
     change name score int;
     重命名表名
      1. rename table employee2 to emp;
      2. alter table employee2 
         rename to  tmp;
     
     增删改查
     insert
     delect
     update
     select
     插入数据
      insert into 表名(列名1,列名2...) values(值1,值2...);
      insert into employee2(id,name) values(1,'kk');
      insert into employee2(id,name,phone_num) values(02,'kk','1516666666');
      //一般id列不用传值
      insert into employee2 values(null,'xx','13945551544',1000);
      insert into employee2 values(null,'张三','132884884',5000);
      insert into employee2 values(null,'张三四','185445284',200);
      insert into employee2 values(null,'李张王飞','1355444524',100);
      insert into employee2 values(null,'章长张','11528452874',220);
      insert into employee2 values(null,'李丽','195554525278',1200);
     删除数据
     delect from employee2 where id =2;
     delect from employee2;清空表,保留表结构(逐条删)
     truncate employee2;清空表,保留表结构(一次性把表删除,相当于drop table,接着创建一样的新空表)
     修改数据
      update employee2  set  name = 'ss'  where  id = 2;
      update employee2 set salary= salary+100 where id = 3;
     查询数据
      select 列名 from 表名 where 条件;
      select *  from employee2;
      select name  from employee2 where  id>2;
      select name,phone_num  from employee2 where  id>2;
      //查询去重distinct
      select distinct salary from employee2;
      //查询之后的列进行运算
      select salary+100 from employee2;
      //查询之后取别名 as (as可以省略)
      select salary+100 as new_salary from employee2;
     
     中文乱码问题
       客户端 服务器
       cmd --> client -->server
       1.临时设置cmd编码
        mysql --dafault-character-set=gbk -uroot -proot
     窗口关闭后失效
       2.永久设置
         修改配置文件 my.ini  57行  utf8改成GBK
      重启mysql  net stop mysql
        net start mysql
     
     
     常用符号
     >  <  =  <>  !=
     in(10,20,30)  
     like '张%'    // %一个或多个字符
     like '%张'
     like '%张%'   //只要包括张的
     like '张_'  // 只能匹配一个通配符
     is null //判断空
     is not null //判断非空
     not
     and
     or

    select* from stu where name = '李四'; 
    select * from employee2 where salary in(1000,200,1200);
    select * from employee2 where name like '张%';
    select * from employee2 where name like '__';//两个下划线表示为两个字

    select * from employee2 where name is null;
    select * from employee2 where salary between  81 and 90;
    select * from employee2 where salary 1000 or  2000;
     
    排序
    order by
    asc 升序(默认)
    desc 降序
    select name,math,english from  stu order by math asc,english desc;
    select name,math+english as sum from stu order by sum desc;别名+排序
    将所有姓张的同学的english成绩降序排列
    select * from stu where name like '张%' order by english desc;
     
     //创建学生表
     create table stu(
     id int primary key  auto_increment,
     name varchar(10) not null,
     age varchar(15) unique ,
     math int,
     english int
     );
     
     insert into stu values(null,'李华','15',60,30);
     insert into stu values(null,'李明','14',80,60);
     insert into stu values(null,'张时','22',40,96);
     insert into stu values(null,'王五','54',66,50);
     insert into stu values(null,'天使','25',25,70);
     insert into stu values(null,'魔鬼','12',90,52);
     insert into stu values(null,'李四','31',98,90);
     
     聚合函数,通常和分组一起使用
     count() 计数
     count(*)
     select count(name) from stu;
     select count(*)  as count from stu;//查询记录取别名
     select count(*) as count from stu where math>80;
     select count(*) as count as sum  from stu where sum>120;
     求和
     sum()
     select sum(math) as sum from  stu;
     select sum(math) ,sum(english) from stu;
      如果使用聚合函数 sum()  null 被当作0;
        select sum(math)+sum(english) from stu;
      当记录某列中有null值时,如果用+相加都为null;
        select sum(math+english) from stu;
        解决方法:使用ifnull(列名,指定默认值)函数解决相加为0问题
        select sum(ifnull(math,0)+ifnull(english,0)) from stu;
    平均值

    avg()
     select avg(math) from stu;
    求max()   min()
     select max(math) from  stu;
     select min(english) from stu;

    //创建销售记录表
    create table orders(
     id int primary key auto_increment,
     product varchar(12) not null,
     date timestamp,
     price int
    ); 
    insert into orders values(null,'洗衣机',null,1000);
    insert into orders values(null,'电视',null,2200);
    insert into orders values(null,'电视',null,2200);
    insert into orders values(null,'洗衣机',null,1000);
    insert into orders values(null,'空调',null,5000);
    insert into orders values(null,'洗衣机',null,1000);
    insert into orders values(null,'空调',null,5000);
    insert into orders values(null,'电视',null,2200);
    分组操作 group by
             group by   having 后面加条件//对group by 后的结果再次进行过滤,不能用where用having
      
    select *  from  表名 where 条件 group by 列名 having分组后的条件 order by 列名desc/asc;
     select product, sum(price) from orders group by product; 
     select product, sum(price) from orders where price>1000 group by product having sum(price) >10000;
     //出错,应该加别名 select product, sum(price) from orders where price>1000 group by product having sum >10000;
     select product, sum(price) as sum from orders where price>1000 group by product having sum >10000;
     select product, sum(price) as sum from orders where price>1000 group by product having sum >10000 order by sum desc;

     
     

     
     

  • 相关阅读:
    jQuery 源码解析(二十四) DOM操作模块 包裹元素 详解
    jQuery 源码解析(二十三) DOM操作模块 替换元素 详解
    jQuery 源码解析(二十二) DOM操作模块 复制元素 详解
    jQuery 源码分析(二十一) DOM操作模块 删除元素 详解
    jQuery 源码分析(二十) DOM操作模块 插入元素 详解
    jQuery 源码分析(十九) DOM遍历模块详解
    python 简单工厂模式
    python 爬虫-协程 采集博客园
    vue 自定义image组件
    微信小程序 image组件坑
  • 原文地址:https://www.cnblogs.com/star521/p/9006393.html
Copyright © 2011-2022 走看看