mysql-

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql-相关的知识,希望对你有一定的参考价值。

- 创建指定字符编码的数据库

         create database sysdb
         default character set utf8;

- 删除数据库
  
        drop database sysdb;

-  查看数据库的默认字符集

        show create database sysdb;

- 修改数据库

        alter database sysdb default character set gbk;


#### 表管理

        use sysdb;

        show tables; //查看所有表
        //建立数据表
        create table student (
        sid int,
        sname varchar(20),
        sage int
        );

        desc student; //查看表的结构
   
        drop table student; //删除数据表

- 添加字段

        alter table student add column sgender varchar(2);
        
- 删除字段

         alter table student drop column sgender;

- 修改表的字段类型
     
         alter table student modify column sname varchar(100);

- 修改数据表的名字

         alter table student change sname(原名) userName(新名) varchar(20);

- 修改表的名称

        alter table student rename to teacher;

###增加数据

- 插入数据

        insert into student values(1 , ‘cx‘, ‘man‘ , 20);

- 插入部分字段

        insert into student(id,name) values(2,‘eric‘);

###修改数据

- 1.修改单个字段

        update student set gender ="女"  //修改所有

        update student set gender ="女" where id = 1;  //修改指定

- 2.修改多个字段
   
        update student set gender = ‘男‘ , age = 30 where id = 1;//逗分隔


- 3.删除表中所有数据

        delete from student;

- 4.带条件的删除

        delete from student where id = 2;


#######delete from  和  truncate table 区别 

- delete from 可以待条件删除,truncate table 不能带条件删除

- delete from 只能删除表的数据,不能删除表的约束,truncate既可以删除表的数据,也可以删除表的约束

##查询数据
- 查询所有列
           
         select * from student;

-  查询指定列
   
        select id,name from student;

- 查询时指定别名(多表查询,别名非常有用)

         select id as ‘编号‘ , name as ‘姓名‘ from student s;//s为表的别名

- 查询时添加常量列

        select id , name , gender , age , ‘年级一‘ as  ‘年级‘  from student;

        查询时加的列为常量列

- 查询时合并列
  
        select id , name , (成绩一+成绩二) as ‘总成绩’ from student
         合并列只能合并数值类型的列

- 查询时去除重复记录

        select distinct sex from stuent;
        //拿到男,女两个结果

- 条件查询(where)

 -  逻辑查询   and  ,  or 
              
              //查询id为2,姓名为李四的学生
               select * from  student   where id = 2  and name = ‘李四‘
             //查询id为2, 或者姓名为张三的学生 
               select * from  student   where id = 2  and name = ‘张三‘


 -  比较条件   > < >= <= <>(不等于)  between and
              
              select * from student where jsp between 75 and 90
          
              select * from student where sex <> ‘女‘

 -  判空  is null / is not null  /  = ‘ ‘ / <> ‘ ‘

             //查询地址为空的学生
  
             select * from student where address is null; //判断null
             select * from student where address = ‘‘; //判断空
             select * from student where address is null or address = ‘ ‘;           

            //查询有地址的学生
             select * from student where address is not null and address <> ‘ ‘;

####重点
 -  模糊条件查询
             
             %表示匹配n个任意字符
             //查询姓张的学生
             select * from name where name like ‘张%‘
             //查询姓‘李‘,且姓名只有两个字的学生
             ­­_匹配一个任意字符
              select * from name where name like ‘李_‘

以上是关于mysql-的主要内容,如果未能解决你的问题,请参考以下文章

MySQL-mysql初涉

MySQL-MySQL基础的语句

MySQL-mysql select可以赋值给变量么

MySQL-mysql 8.0.16安装教程

MySQL-mysql 8.0.11安装教程

MYSQL-mysql 8.0.11安装教程