MySQL基础语法笔记

 1.数据类型  

  • 数值类型

   1> 整数:

整数类型 字节 范围(有符号) 范围(无符号)
tinyint 1 -128—127 0—255
smallint 2 -32,768—32,767 0—65,535
mediumint 3 -8,388,608—8,388,607 0—1,677,215
int、integer 4 -2,147,483,648—2,147,483,647 0—4,294,967,295
bigint 8 -9,223,372,036,854,775,808—9,223,372,036,854,775,807 0—18,446,744,073,709,551,615

  

     整数类型常见属性设置:   

     1) UNSIGNED :无符号,可以有双倍的上限数值。

     2) auto_increment :自增(默从1开始),必须配合主键使用。

     3) primary key :主键 ,可以唯一标识某条记录的一个字段或者多个字段。

       设置:primary key (字段名);  , primary key(字段1,字段2) ; ,create table 表名 (字段1 字段1类型 primary key , ......  ) ;  , alter table 表名 add primary key (字段名) ; 

       删除:alter table 表名 drop primary key ;   若字段具有auto_increment属性则必须先删除这个,才能把primary key删掉。

     4) null :  规定字段的值是否可以为null 或  not null  。可用以其他类型。

     5) default :  默认值 。设定默认值后,若省略不写则以默认值补上。可用于其他类型。

   2> 小数:

     1) 浮点数:

       单精度 float:精确到7位(整数位+小数位) ,有 (M,D) 方式来规定M:整数位+小数位位数 和 D:小数位位数,超过部分四舍五入。

       双精度 double:精确到15位(整数位+小数位) ,有 (M,D) 方式来规定 M: 整数位+小数位 和 D:小数位位数,超过部分四舍五入。

     2)定点数(更精确):

       定点数 decimal :  在MySQL内部以字符串保存。 有 (M,D) 方式来规定 M: 整数位+小数位 和 D:小数位位数,超过报错。

  • 日期类型

   datetime 是最常用的,返回年月日时分秒。

     create table time(dt datetime) ;  insert into time values(now( ) ) ;  

  • 字符串类型

   1> 保存少量字符串时:

       1) char (M)  :  M为0~255之间的整数,表示可以存M改为字符。在创建表时声明长度。

      2) varchar (M) : M为0~65535之间的整数,表示可以存M个字符。

   2> 保存大文本时:

     1) text : 允许长度为 0~65535个字符。

     2) tinytext : 允许长度为0~255个字符。

     3) mediumtext : 允许长度为0~16,777,215个字符。

     4) longtext : 允许长度为0~4,294,967,295个字符。

   3> ENUM 枚举类型:从允许值集合中选取单个。单选!    ( create table t (sex  enum('man' , 'woman' ) ; ))

   4> SET类型:从允许值集合中任选一个或多个。多选!     ( create table dinner ( 'beef' , 'noodle' ,'pizza ' ,'hamburger') ; )

     FIND_IN_SET函数对SET类型数据进行查询。可以很快查出在SET中有某一个值得组合。 ( select * from dinner where find_in_set ('noodle' , 'dinner') ; )

2.运算符   

  • 算术运算符:+ , - , * , / , %
  • 比较运算符 :    = 等于,  <> 或 != 不等于,   <=> 等于(可以用于null值得比较) ,   

          <  , <=  , > , >= , between 存在指定范围 (select 10 between 10 and 100 ; )

           in 存在于指定集合 ,    is null 为NULL  ,is not null  不为NULL ,

          like 通配符(%)匹配 ( select 'abcdefg ' like ' abc% ' ; )   ,  regexp 或 rlike 正则表达式匹配  (select ‘abcd’ regexp ' ^a' ; )

  • 逻辑运算符:NOT 或 !  逻辑非 ,   AND 或 &&  逻辑与 ,   OR 或 ||   逻辑或    ,   XOR  逻辑异或 

3.常用函数  

  • 字符串函数 

   1> concat (s1,s2,...) :  连接为一个字符串。

   2> insert (str,x,y,instr )  :  将字符串str 从第x位置开始,y个字符长的字符串换成 instr 。

      3> lower (str)  :  将字符串str 中所有字符变为小写。

   4> upper (str) :   将字符串str 中所有字符变为大写。

   5> left (str , x)  :  返回字符串strx最左边的x个字符。

   6> right (str , x)  :  返回字符串strx最右边的x个字符。

   7> replace (str ,a ,b ) : 用字符串b 替换字符串str 中所有的字符串a 。

  • 数值函数

   1> abs (x)  :  返回x 的绝对值。

   2> ceil (x) :  返回大于x 的最小整数值 。

   3> floor (x)  :  返回小于x 的最大整数值。

   4> mod (x,y) :  返回 x/y 的模,即取余。

   5> rand ()  :  返回0-1内的随机值。

  • 日期和时间函数

   1> curdate() :  返回当前日期。

   2> curtime ()  :  返回当前时间。

   3> now () :  返回当前日期时间。

   4> unix_timestamp(date)  :   返回日期date 的unix时间戳。   ↓搭配起来更配哦

   5> from_unixtime (时间戳,要转换的格式) :   返回unix 时间戳的日期值。 

  • 流程函数

   1> if (value ,t ,f)  :  如果value 是真,返回t ,否则返回f 。

   2> ifnull (value1, value2)   :  如果value1 不为空,返回value2 ,否则返回default。

   3> 简单函数:case when [value1] then [result1] ... else [default] end  :   如果value1 是真,返回result1 ,否则返回default 。

  枚举这个字段所有可能的值;

  

 4>搜索函数: case [expr] when [value1] then [result1] ... else [default] end  :  如果expr 等于value1 ,返回result1 ,否则返回default 。

  可以写判断,只返回第一个符合条件的值,其他的cass被忽略

  • 其他

   1> datebase ()  :  返回当前数据库名。

   2> version ()   : 返回当前数据库版本。

   3> inet_aton (ip)  :  返回IP地址的数字表示。

   4> inet_ntoa (num) :  返回数字代表的IP地址。

4.语句分类

 DDL语句:对数据库内部对象进行创建、删除、修改等操作的语句。

  • 语句以 ; 或 g 结尾
  • 查看数据库列表: show databases ;  
  • 创建数据库:

   1> 创建数据库:  create database 数据库名 ; ( create database STUDENT ; ) 

   2> 选中要操作的数据库:  USE 数据库名 ;     ( USE STUDENT;  )

   3> 查看数据库中的数据表(先选中数据库):   show tables ; 

  • 删除数据库:

    删除数据库: drop database 数据库名 ;

 

   下列操作的前提先选中数据库

  • 创建表:  < > 表示可省略内容

 1> 创建表: create table 表名(字段1名 字段1类型 < 列的约束条件 > , 字段2名 字段2类型 < 列的约束条件 >, ......  ) ;

           ( create table student (id1 int , id2 int ) ; )

    列的约束条件默认值为: default null ,可省略不写。

   2> 查看表的定义(创建完表后): desc 表名 ;

 3> 查看创建表时的SQL语句: show create table 表名 G

   G 使记录能够按照字段竖向排列,显示效果更好。G之后不用分号 (加上分号会报错)

  • 删除表:

 删除表: drop table 表名 ;

  • 修改表:

 1>  修改表的字段类型(int改为tinyint) :   alter table 表名 modify <column> 字段名 字段类型 <first  | after 字段名> ;   ( alter table student modify  id1 tinyint ; )

 2>  增加表字段alter table 表名 add <column> 字段名 字段类型 <first | after 字段名> ;        ( alter table student add id3 int ; )     

 3>  删除表字段 :  alter table 表名 drop <column> 字段名 ;       ( alter table student drop id3  ; )  

 4>  字段改名 :  alter table  表名  change  <column> 旧的字段名 新的字段名 新的字段类型  <first | after 字段名> ;     ( alter table student change id2 di3 int ; )

         change 和 modify 都可以修改字段类型,但change 还可以修改字段名

 5>  修改字段顺序: 在以上语法中 <first  | after 字段名>  的作用是修改顺序,在哪个字段前或后。   ( alter table student modify id1 tinyint after id2 ; ) ( alter table student add id4 int first ; )

         只能after 字段名 ,first 字段名 会报错。

 6> 更改表名 : alter table 旧表名 rename <to> 新表名 ;  

 DML语句:对数据库中表记录的操作,包括表记录的插入、更新、删除查询。是使用最频繁的语句。

      先创建一个数据库和一张简单的表 :

     create database data ;

     USE data;

     create table tab1 (id int ,age tinyint) ;

  • 插入记录 :

 1> 插入记录 :insert into 表名 < (字段1,字段2,......) > values(值1,值2,......) ;      ( insert into student values (1,20); )   ( insert into student (age,id) values (25,2);  )

 2> 一次插入多条记录:insert into 表名 < (字段1,字段2 ,......) > values (值1,值2,......) ,(值1,值2 ,......) , ......  ;             ( insert into student (id,age) values (3,12),(4,87),(5,33); )

  • 更新记录:

   1> 更新一个表:update 表名 set 字段1=值1,字段2=值2,...... < where 条件 > ;     ( update student set age=100 where id=1; )

   2>更新多表中数据: update 表1,表2,...... set 表1.字段1=表达式1,表n.字段n = 表达式n <where 条件>

    ( 再创建一个和student一模一样的表employee,    update student,employee set student.age=1000,employee.age=20000 where student.id=1 and employee.id=1 ; )

  •  删除记录:

           1> 删除单表中的记录: delete from 表名 where 条件 ;                 ( delete from student where id=3; ) ( delete from student where age=20; ) ( delete from student where age=20 or id=5 ; )

   2> 删除多表中的记录: delete 表1,表2,...... from 表1,表2,...... where 条件 ;          ( delete  student,employee from student,employee where student.age=20 and employee.id=1; )

如果不加where 条件,则表中所有记录被删除。

  • 查询记录:

   select  *  <别名> from 表名 where 条件 ;  查询时可以为字段编辑别名,适用与php配合。

   1> 查询不重复的记录:select distinct 字段1,字段2from 表名 < where 条件 > ; (有distinct ,字段1,字段2中任何字段有不同就会被选择。)      ( select distinct id from student ; )  

   2> 条件查询: or ,and, = , >, <, >= , <= ,!= 等比较运算符等

  • 排序和限制:

     1> 排序:select * from 表名 < where 条件 > order by 字段1 DESC/ASC ;     ( select * from student order by age DESC ;  )   

     默认ASC:由低到高 。

        select * from 表名 < where 条件> order by 字段1 DESC/ASC,字段2 DESC/ASC, ...... ;   

     在字段1有重复情况下,按照后续字段依次排序。

    2> 限制:对于排序后的数据若只希望显示一部分,在查询语句后面使用 LIMIT start(开始处),row(条数) 来限制。

       ( 比如只希望显示从头开始的3条数据,select * from student order by age desc limit 3 ;

        比如只希望显示第3条至第5条数据,  select * from student order by age desc limit 2,3 ; )

  • 聚合:

   1> sum求和: select sum(字段名) from 表名 ;      ( select sum(age) from student ; )

   2> count 记录总数:select count (字段名) from 表名 ;      ( select count(*) from student ; )

   3> max 最大值: select max(字段名) from 表名 ;            

   4> min 最小值 : select min (字段名) from 表名 ;            ( select min(age) from student ; )

   5> group by 分组 :select 字段名 from 表名 group by 字段名 < with rollup> ;

      < with rollup>  对分类结果进行再汇总。  select sum(salary) from employee group by department ; 和  select department,sum(salary) from employee group by department ;  加上一个 with rollup 可以对分组结果进行汇总。                       

   6>  having 条件过滤:搭配 group by 。group by 不能与where搭配。

      ( 筛选部门总薪水大于1000的部门,select department,sum(salary) from employee group by department having sum(salary) > 10000 ; ) 

  • 表连接:     #8fbc8f
  • 1> 内连接:选取两站表中相互匹配的部分。  (select id,department ,salary from student , employee where student.id = employee.id ; )

    2> 外连接:

       左连接:包含左边表中的所有记录,右表依附左表,没有的填NULL。      (select student.id,department ,salary from student left join employee  on where student.id = employee.id ; )

       右链接:包含右边中的所有记录,左表依附于右表,没有的填NULL。

  • 子查询:一个查询需要调用另一个查询的结果。

   1>  in :in后面的子语句必须只返回一个字段,若查询结果唯一(只有一条时)可以使用 = 来代替 in 。

        ( select * from employee where id in ( select id from from employee_late ) ; )   调用查询—返回迟到人员id的查询结果。

   2> not in :与 in 相反。

   3> exists :exists前的语句中每一项与后面的子语句进行匹配,能这一项能匹配上返回true,匹配不上返回false,根据子语句返回的结果输出。

        ( select * from employee where exists ( select * from employee_late where employee_late.id = employee.id) ; )   employee表中的第一项即id=1的员工与late迟到表中的id相比较,若有,返回的是true,那么把id=1 员工在employee表中的所有信息打印,若没有,前进到下一项id=2......

     4> not exists:与exists 相反。

  • 记录联合:将两个或多个表按照要求查询出的结果合并到一起显示。

     1> union : 将多个查询结果合并并去重后再返回。

     2> union all :直接合并。

    union , union all 合并的条件是:要合并的结果,它们的列数必须相同。

 DCL语句:数据控制语句,可对数据库相关权限进行设置。

5.phpMyAdmin 

  phpMyAdmin用于数据库管理,数据对象管理,用户管理,数据导入导出等。

  

原文地址:https://www.cnblogs.com/mingnai/p/12488025.html