MySQL

MySQL数据库

一.数据库的基本概念

  1. 数据(Data)对客观事物的符号表示。为数据库存储的基本对象。
  2. 数据库(Database. DB):按照数据结构来组织、存储和管理数据的仓库。

数据库管理系统DBMS。保证数据的安全性和完整性。

MySQL数据库。小型关系型数据库管理系统

 

二.MySQL数据库的优缺点

1.优点:

MySQL源代码免费下载(开源);体积小,便于安装(简单);性能优越;功能强大

2.缺点:

不能直接处理XML数据(有序文本形式);一定功能上支持不够完善;不支持实时分析系统(OLAP) 。

 

 

三.连接MySQL的命令语句

Windows+R打开运行对话框 输入cmd开启管理员MySQL对话框。

MySQL –h localhost –u root –p 回车。

Show databases;显示数据库。

Use 数据库名称;使用数据库。

Show tables;显示表格。

Create database 需创建的数据库名称;创建新的数据库。

Show create database 数据库名称;显示创建的数据库的相关信息。

Drop database 欲删除的数据库名称;删除数据库。

Alter database 数据库名称 default character set utf8;编码改为utf8。

Create database 数据库名称 default character set gbk; 编码默认为gb2312。

Alter table 欲修改的数据表名称 rename/rename to 新表名;修改表名。

  Alter table 欲添加的数据表名称 add 新字段名 其数据类型 约束;添加字段。

Alter table 数据表名称 change 旧字段名 新字段名 数据类型;修改字段名。

Alter table 数据表名称 drop 欲删除的字段名;删除字段。

Alter table 数据表名称 add constraint 约束名 

foreign key(字段名) references 另一数据表(字段名);事后添加外键。

Alter table 数据表名称 modify 字段名 欲改为的数据类型 约束;事后添加约束。

Alter table 数据表名称 drop foreign key  自定义的约束名称;删除外键。

Alter table 数据表名称 modify 字段名 欲改为的数据类型;修改数据类型。

Insert into 数据表名称(字段,) values (欲添加的数据表内容,用逗号相隔);添加数据。

Select * from 数据表名称;显示数据表中的详细数据。

Select * from 数据表名称 where 条件 limit 0,1;显示符合条件的数据内容。

Select count(*) from 数据表名称 where 条件;显示符合条件的个数。

Update 数据表名称 set sname=’***’, sage=18 where sage=19; 

Delete from数据表名称 where sname=’***’;删除sname=‘***’的数据。

Select avg(sage) from 数据表名称;求该表中sage的平均数。

Show create table 数据表名称;显示数据表信息。

Select sno.cno from  score  order by degree desc limit 0,1;取出第一名。

 

四.MySQL数据库的列类型(字段类型)

分为三种:数值型,字符串型,日期/时间型。

  1. 数据型分为整型和浮点型。

Tinyint(3)    smallint    mediumint    int   bigint

Float(10,4)      double     decimal(5,2)

  1. 字符串型

Char     varchar     blob(存储图片二进制码)  text(非二进制的存储类型)

 Char与varchar的区别:char定长,varchar不定长,字符串定长时用char。

  1. 日期/时间型

Date     time  timestamp

00~69解析成2000~2069年;70~99解析为1970~1999年。

 

五.约束

主键约束  id int   primary key

外键约束   constraint

唯一约束   name varchar(10) unique

非空约束   age tinyint(3)  not null

默认约束   price date default ‘2016-03-18’

  1. 主键可以确定唯一标识的数据,要求数据唯一,且不为空。多字段联合主键primary key(字段名1,字段名2) 主键递增 auto-increment

Create table tushu(

Id int primary key auto_increment,

Name varchar(10)

)auto_increment=901;

Alter table tushu auto_increment=901;

  1. 外键用来在两个表的数据之间建立连接。

Constraint 自定义约束名 foreign key(本表中的字段名) references 另一表名(字段名);

将本表的字段名作为外键与另一表中的主键(另一表单)相关联。

  1. 唯一约束可以确保一列或者几列不出现重复值。

 

六.数据查询

Select * from 欲查询的数据表名称;显示欲查询数据表的所有数据。

  Select 字段名,字段名 from 数据表名称;显示字段和字段的数据。

Select * from 数据表名称 where 条件;显示符合条件的数据。

Select 字段名 from表名 where 条件;显示表中字段符合条件的数据。

Select 字段名 from表名 where 字段名 between 0 and 5000;  显示表中字段中满足字段在0~5000间的数据。

(包括0和5000)

Not between and 不在……范围内

Select 字段名,字段名 from 表名 where 条件 or 条件 or 条件; 显示表中满足三条件之一的数据。

=select 字段名,字段名 from表名 where in(条件,条件,条件);

  • 模糊查询

Select 字段名 from 表名 where 字段名 like ‘%王’;显示表中字段名符合王%的数据。

%为任意 _为单个字符

Select 字段名 from 表名 where 字段名 is null;表中字段为null的数据。

空值null  空字符串值(\0)

Select  字段名 from表名 where 字段名 =‘ ’;表中字段为  的数据。

select distinct 字段名 from 数据表名;去除重复值,显示数据。

Select * from 数据表名称 order by 字段名 desc;将数据表中字段以降序排序。

Desc 从大到小  asc 从小到大(默认的排序方式)

Select * from 数据表名称 order by 字段名1 desc,字段名2 asc;将数据以字段1降序排列,若有数据重复,将字段2降序排列。

Select * from 数据表名称 limit 起始值,取值的个数;显示起始值开始的一定个数的数据。

Limit 限制的是条数。

Select * from 数据表名称 order by 字段名 desc limit 起始值,个数;

Select  year(now()) – year(sbirthday) from 表名 where 条件;查询年龄。

 

Select 字段名/聚合函数 from 数据表名称 where 条件 group by 字段名 having 筛选条件 order by desc/asc limit  m,n;

 

七.函数

1.count()计数函数

Select count(*) from 数据表名称 where 条件;

2.sum()求和函数

select sum(字段名) from 数据表名称;

 3.avg()求平均数

Select avg(字段名) from 数据表名称;

 

Select avg(degree) from score group by cno;

Select cno,avg(degree) from score group by cno;

  1. max(),min()

select * from 数据表名称 where salary = (select Max(salary) from 表名);查询数据中的最高工资。

 

表连接 join

 Select  * from 数据表名称1  inner join 数据表名称2  on 表1.字段名 = 表2.字段名 where 条件;

 两表辅助查询符合条件的数据信息。

 Select * from 数据表名称1 inner join 数据表名称2 on 链接条件  inner join 表3 on 连接条件;

 

Group by 分组后数据重新定义,具有其局限性。与聚合函数连用。

Having 与group by 连用。 进行筛选,后加条件。

例: select count(*) from score where cno like ‘3%’group by cno having  count(*) >=5;

 

  • 删除数据

Delete from 数据表名称 where 条件;Delete from 表名 删除数据保留索引值。

Truncate table 数据表名称; 清空表,同时删除索引值。

  • 数据修改

 Update 数据表名称 set 需修改的字段名 = ‘修改后内容’(多者,以逗号相隔) where 条件;

  • 插入数据

 Insert into 数据表名称 value();

Insert into 数据表名称 (字段名) value ();

Insert into 数据表名称 (字段名) values ();

 

Insert into 新数据表名称(新字段名) select 字段名 from 数据表名称;

将数据表的字段查询出,插入到新数据表的新字段名的位置。

 

 
数据定义语言(DDL)
 
Data Definition Language 
 
如Create drop alter
 
<1>.create databses <数据库>
<2>.create table <数据表>
 
<a>.drop database <数据库名>
<b>.drop table if exists < 数据表>
<c>.删除被其他表关联的主表;
一,先删除关联子表的外键约束:alter table <表名> drop foreign key<外键约束,即自己起的名字>
二,删除主表:drop table <主表>
<A>.修改表名:alter table<旧表名>rename to <新表名>to为可修该参数,使用与否不影响结果。
<B>.修改字段的数据类型:alter table<表名>modify<字段名><数据类型>
<C>.修改字段名:alter table<表名> change<旧字段名><新字段名><新数据类型>
<D>.添加字段:alter tabel <表名> add <  新字段名><数据类型>
<E>.在表的第一列添加字段:alter tabel <表名> add <  新字段名><数据类型>
<F>.在指定列之后添加字段:alter tabel <表名> add <  新字段名><数据类型>after<被指定的列之后>
<G>.删除字段:alter table <表名>drop <字段名>
<H>.更改表的存储引擎:alter table<表名> engine=更改后的存储引擎
数据操纵语言
Manipulation Language
 
如 INSERT UPDATE DELETE
 
 
<1>.insert into <数据表><字段1,字段2,字段3,.....字段n>values<'','',''>
 
<a>.修改语句:update<表名> set 字段名=“值” where
 
<A>.删除语句:delete from <表名> where
 
 
数据查询语言(DQL)
 
Data Query Language
 
SELECT
 
 
<1>.select * from <数据表>
 
事务控制语言(TCL)
 
Transaction Control Language
 
COMMIT ROLLBACK
 
 
  
数据控制语言(DCL)
 
Data Control Language
 
 grant  revoke
 
 
 
 
 
 
 
修改表结构(ALTER TABLE语句)
 
 
alter table test add  column name varchar(10); --添加表列
 
alter table test rename test1; --修改表名
 
alter table test drop  column name; --删除表列
 
alter table test modify address char(10) --修改表列类型
 
alter table test change address address  char(40) --修改表列类型
 
alter table test change  column address address1 varchar(30)--修改表列名
 
[CONSTRAINT symbol] FOREIGN KEY [id]
 (index_col_name, ...)
 
 REFERENCES tbl_name (index_col_name, ...)
 
[ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
 
[ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
mysql是最流行的关系数据库管理系统.
 
 
什么是数据库?
    数据库是按照数据结构组织,存储和管理数据的仓库。
    关系型数据库是建立在关系模型基础上的数据库。
 
 
关系数据库关系系统的特点:
    1.数据以表格的形式出现;
    2.每行为各种记录的名称;
    3.每列为记录名称所对应的数据域;
    4.许多行和列组成了表;
    5.若干表组成了数据库;
 
 
术语:
    数据库:数据库是一些关联表的集合;
    数据表:表是数据的矩阵;
    列:一列包含相同的数据;
    行:是一组相关的数据;
    主键:是唯一的,可以用主键来查询数据;
    外键:用于关联两个表;
 
 
mysql数据库的优势:
    1、开源的,免费的;
    2、支持大型的数据库;
    3、使用标准的SQL数据语言形式;
    4、可以允许于多个系统上,并且支持多种语言;
    5、对PHP有很好的支持,PHP是目前最流行的Web开发语言;
    6、是可以定制的;
 
数据类型:
    字符型:
        char 255字节
        varchar 65535
        tinytext255
        text 65535
        longtext42亿字节数
    整型:
        bigint  8字节  19     -2的63次方~2的63次方-1;
        int  4字节  10        -2的31次方~2的31次方-1;
        mediumint  3字节 7    -2的23次方~2的23次方-1;
        smallint  2字节  5    -32768~32767;
        tinyint  1字节  3    -128~127;
    浮点型:
        float 4字节
        double 8字节
        decimal
    日期时间型:
        date  1996-12-15     1000-01-01~9999-12-31
        time  11:11:11     00:00:00~23:59:59
        datetime  1996-12-15 11:11:11      1000~9999
        year 1996        1901~2055
        timestamp 19961215123015    1970~2037
 
    枚举:enum('M','W');   只能插入給定值M,W,并且只能插入一个给定值;
    集合:set('1','2','3');只能插入给定值123,可以插入多个给定值;
 
char和varchar的区别:
    1、变长和定长;
    2、varchar会执行判断,消耗一定系统资源;
 
 
mysql -uroot -p 输入密码;
 
show databases;显示数据库;
 
    create database databasename;创建数据库;
    drop database databasename;删除数据库;;;
 
    if not exists;如果不存在;
        create dlllltabase if not exists databasename;如果不存在就创建;
        drop dtabase if exists databasename;如果存在就删除;
 
 
use databasename;使用数据库;
 
 
 
show tables;显示表;
 
    create table tablename(字段 数据类型 列选项,字段2 数据类型 列选项);创建表;
        列选项:
            null/not null;是否可以为空;
 
            default 1;默认值是什么;
 
            comment '备注';备注;
 
            primary key;主键;
 
            foreign key;外键;
                constraint fk_emp_dept1 foreign key(deptid) references tb_dept1(id)
                在表tb_emp5上添加了名称为fk_emp_dept1的外键约束,外键名称为deptId,其依赖于表tb_dept1的主键id。
                级联(cascade)方式:
                    1:cascade;
                    2:set null;
                    3:no action;
                    foreign key (groupid) references t_group(id) on delete cascade on update cascade
 
            unique;唯一;
 
            zerofill;用0填充;
 
            unsigned;不允许带符号(不允许负数);
 
            auto_increment;自增数;
                自增数:
                    如果想使用自增数,必须将其设置为主键;
                    create table if not exists tablename(id int not null auto_increment,primary key(id));将id设置为主键;(创建表时设置)
                    create table if not exists tablename(id int not null auto_increment primary key);(创建表时设置)
                    alter table tablename add id int not null auto_increment primary key;    新添加一列id并设置为主键;(添加新列时设置)
    drop table tablename;删除表;
 
    if not exists;
        create table if not exists tablename(字段 数据类型 列选项,字段2 数据类型 列选项);如果不存在就创建;
        drop table if exists tablename;如果存在就删除;
 
show create table tablename;显示创建表时输入的SQL语句;
 
desc tablename;显示表的结构;
 
alter table tablename;修改表结构;
    新建列:
        add 列名 数据类型 列选项;
            after 列名;将新加的列插入到列名后面;
            alter table tablename add lie char(10) not null after lie2;新插入列 lie 插入到lie2后面;
            alter table tablename add lie char(10) not null first;将新列插入到第一位;
 
    修改列(功能更强大,可以修改列名):
        change 旧列名 新列名 数据类型 列选项;
        alter table tablename change name name2 char(10) not null;旧列名,新列明不能少;
 
    修改列(不可以修改列名)
        modify 列名 数据类型 列选项;
        alter table tablename modify name char(10) not null;
 
    修改默认项:
        alter set default 1/drop default          新加默认/删除默认;
        alter table tablename alter set default 1;
 
    删除列:
        drop
        alter table tablename drop name;
 
        删除主键:
            alter table tablename drop primary key;
        删除唯一(unique):
            alter table tablename drop index 唯一名(unique 的名称);
        删除外键:
            alter table tablename drop foreign key(外键名称);
 
    修改表名:
        rename
        alter table tablename rename tablename2;
 
    alter table tablename add a1 char(10),add a2 char(10),add a3 char(10);创建多列;
 
 
insert增加数据;
    insert into tablename values(字段对应的数据,字段对应的数据2……);插入数据;字段的数据与创建表的字段对应;
 
        insert into tablename(字段,字段2,字段3) values(字段对应数据,字段2对应数据,字段3对应数据);字段的数据和前面指定的字段对应;
 
        insert into tablename values(),(),();   插入多条数据;
 
    peplace into tablename values();插入数据;
 
        peplace和insert用法基本相同。不同在于,peplace可以在插入数据之前将于新纪录有冲突的旧记录删除,之后再插入;
 
 
delete删除数据;
    delete from tablename;清空表;
        truncate table tablename 清空表;和delete的区别在于 truncate可以重置自增数;
    delete from tablename where id=1;
 
 
update修改数据;
    update tablename set name='';
    update tablename set name='',sex='男',nl=18;
    update tablename set name='' where id=2;
 
 
select查询数据(重);
    select * from tablename;查看表内所有内容;
 
        select 字段名,字段名2 from tablename;查看表内某字段的内容;
 
    where name=‘某某某’;条件 name=‘某某某’;
        select * from tablename where name='某某某';将表内 name为‘某某某’的数据显示出来;
 
        select * from tablename where id>3 and id<10;将id大于3且小于10的显示出来;
 
        select * from tablename where id<3 and id>10;这种写法是不会显示内容的,因为既小于3又大于10的数找不到;(and表示两个条件同时满足,并不是小于3和大于10的显示);
 
        select * from tablename where id<3 or id>10;小于3或大于10的显示(小于3和大于10的都会显示);
 
        select * from tablename where name in('名字1','名字2');名字为‘名字1’和‘名字2’的会显示;
 
        select * from tablename where name like '%赵';名字中含有赵的会显示(%号代表0或者多个字符,_代表一个字符)——('%赵%'只要字符串中含有赵 就会显示),(‘%赵’只能赵的前面有字段,后面没有  才会显示),('_赵'赵的前面只有1个字符,后面没有  才会显示);
 
        select * from tablename where name is null;name是null的会显示;
 
        select * from tablename where name is not null;name不是null的显示;
 
    group by zu;分组;
        select count(id) from tablename group by zu;显示分组数量;
            count 数量
            sum 求和
            avg 平均数
            max 最大
            min 最小
            round 四舍五入
            floor 向下取整
            ceil 向上取整
 
    having id=2;(和where用法相同,where用在分组之前,having用在分组后)
        select count(id) from tablename group by zu having id=2;
 
    order by;排序(默认为正序排列)有两个参数 asc desc(正序和倒序);
        select count(id) from tablename group by zu order by count(id) desc;
 
    limit 0,10;分页
        select * from tablename limit 0,10;第一页    从第1条数据开始,显示10条;
        select * from tablename limit 10,10;第二页   从第11条数据开始,显示10条;
        select * from tablename limit 20,10;第三页   从第21条数据开始,显示10条;
 
        as 定义别名;
        select name as '姓名' from student;字段名不显示name而是‘姓名’
 
    select count(id) as '数量' from tablename where id<10 group by zu having count(id)<6 order by desc limit 0,10;
 
     select student.name as '学生姓名',school_name.name as '学校' from school_name,student where school_name.id=student.id;查询两个表(
    select tablename.columnname,tablename2.columnname2 from tablename,tablename2 where tablename.**=tablename2.**;找到两个表有关联的项作为条件;
    )
 
 
 
 
默认当前时间;
 
    default current_timestamp;默认当前时间戳;
 
    on update current_timestamp;修改时自动更新为当前时间戳;
 
    在create table语句中,可以用下面的任何一种方式声明第1个timestamp列:
 
        用default current_timestamp和on update current_timestamp子句,列为默认值使用当前的时间戳,并且自动更新。
 
        不使用default或on update子句,与default current_timestamp on update current_timestamp相同。
 
        用default current_timestamp子句不用on update子句,列为默认值使用当前的时间戳但是不自动更新。
 
        不用default子句但用on update current_timestamp子句,列有默认值0并自动更新。
 
        用常量default值,列有给出的 默认值。如果列有一个on update current_timestamp子句,它自动更新,否则不。
 
 
在创建表的最后可以修改表的存储引擎和表的默认编码:
 
    存储引擎engine:myisam,innodb;默认为innodb;
 
    编码 default charset;
 
    create table if not exists tablename(
             id int auto_increment primary key,
             name char(10) not null
             )engine=myisam default charset=utf8;
 
 
时间戳
select unix_timestamp();
时间戳转化为现在时间
select  from _unixtime(unix_timestamp())
取年份
86400 为一天
select  from _unixtime(unix_timestamp()+right(rand(),7))
s_date=  left(from _unixtime(unix_timestamp()+right(rand(),7)),10);
MONTH()
YEAR()
时区的设置
default
外键:
constratint fk_b foreign key (从表的字段) references 主表 (主表的字段); 
 

 

 

 

SELECT 语句

 

一.简单查询(重点)

  1. 全部字段查询

Select * from 表明

  1. 单字段查询

Select 字段 from 表明;

  1. 多字段查询

Select 字段1,字段2.。。 From 表明;

  1. 定位查询

Select * from 表明 where 条件;

!= <>  =   <=  >=   >  <

  1. 多条件查询

And  or

Select * from 表明 where 条件一 and 条件二;

Select * from 表明 where 条件一 or 条件二;

Select * from 表明 where 字段 between 值  and 值;

Select* from 表明 where 字段 in(值,值。。。);

  1. 模糊查询

Select * from 表明 where 字段  like ‘%’;

Select * from 表明 where 字段 like ‘_’

表示字符0---

表示一个字符

  1. 空值查询

Select * from 表明 where 字段 is null

Select * from 表明 where 字段 not is null

Select ifnull(字段,默认值) from 表;

  1. 查询结果不重复

Select distinct 字段名  from  表明

  1. 排序

Select * from 表明 order by 字段 排序方式(ASC, DESC);

  1. 分组

Select * from 表明 group by 字段  having 条件;

Select * from 表明group by 字段 with rollup

  1. 限制查询结果数量

Select * from 表明 limit 开始条数,条数;

 

  1. 函数

聚合函数(重点)

聚合函数是用来做纵向运算的函数:

   Count()计算记录数

  • COUNT():统计指定列不为NULL的记录行数;
   Max() 求最大值
  • MAX():计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运算;
   Min() 求最小值
  • MIN():计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算;

  Sum() 计算总和

 
  • SUM():计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为0
   Avg() 求平均值
  • AVG():计算指定列的平均值,如果指定列类型不是数值类型,那么计算结果为0

 

流程控制函数

1、CASE

2、IF

3、IFNULL

4、NULLIF

数学函数

ABS(x)   返回x的绝对值
BIN(x)   返回x的二进制(OCT返回八进制,HEX返回十六进制)
CEILING(x)   返回大于x的最小整数值
EXP(x)   返回值e(自然对数的底)的x次方
FLOOR(x)   返回小于x的最大整数值

GREATEST(x1,x2,...,xn)返回集合中最大的值

 

LEAST(x1,x2,...,xn)      返回集合中最小的值
LN(x)                    返回x的自然对数
LOG(x,y)返回x的以y为底的对数
MOD(x,y)                 返回x/y的模(余数)
PI()返回pi的值(圆周率)
RAND()返回0到1内的随机值,可以通过提供一个参数(种子)使RAND()随机数生成器生成一个指定的值。
ROUND(x,y)返回参数x的四舍五入的有y位小数的值
SIGN(x) 返回代表数字x的符号的值
SQRT(x) 返回一个数的平方根
TRUNCATE(x,y)            返回数字x截短为y位小数的结果

 

字符串函数

ASCII(char)返回字符的ASCII码值
BIT_LENGTH(str)返回字符串的比特长度
CONCAT(s1,s2...,sn)将s1,s2...,sn连接成字符串
CONCAT_WS(sep,s1,s2...,sn)将s1,s2...,sn连接成字符串,并用sep字符间隔
INSERT(str,x,y,instr) 将字符串str从第x位置开始,y个字符长的子串替换为字符串instr,返回结果
FIND_IN_SET(str,list)分析逗号分隔的list列表,如果发现str,返回str在list中的位置
LCASE(str)或LOWER(str) 返回将字符串str中所有字符改变为小写后的结果
LEFT(str,x)返回字符串str中最左边的x个字符
LENGTH(s)返回字符串str中的字符数
LTRIM(str) 从字符串str中切掉开头的空格
POSITION(substr,str) 返回子串substr在字符串str中第一次出现的位置
QUOTE(str) 用反斜杠转义str中的单引号
REPEAT(str,srchstr,rplcstr)返回字符串str重复x次的结果
REVERSE(str) 返回颠倒字符串str的结果
RIGHT(str,x) 返回字符串str中最右边的x个字符
RTRIM(str) 返回字符串str尾部的空格
STRCMP(s1,s2)比较字符串s1和s2
TRIM(str)去除字符串首部和尾部的所有空格
UCASE(str)或UPPER(str) 返回将字符串str中所有字符转变为大写后的结果

 

日期时间函数

CURDATE()或CURRENT_DATE() 返回当前的日期
CURTIME()或CURRENT_TIME() 返回当前的时间
DATE_ADD(date,INTERVAL int keyword)返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化),如:SELECTDATE_ADD(CURRENT_DATE,INTERVAL 6 MONTH);
DATE_FORMAT(date,fmt)  依照指定的fmt格式格式化日期date值
DATE_SUB(date,INTERVAL int keyword)返回日期date加上间隔时间int的结果(int必须按照关键字进行格式化),如:SELECTDATE_SUB(CURRENT_DATE,INTERVAL 6 MONTH);
DAYOFWEEK(date)   返回date所代表的一星期中的第几天(1~7)
DAYOFMONTH(date)  返回date是一个月的第几天(1~31)
DAYOFYEAR(date)   返回date是一年的第几天(1~366)
DAYNAME(date)   返回date的星期名,如:SELECT DAYNAME(CURRENT_DATE);
FROM_UNIXTIME(ts,fmt)  根据指定的fmt格式,格式化UNIX时间戳ts
HOUR(time)   返回time的小时值(0~23)
MINUTE(time)   返回time的分钟值(0~59)
MONTH(date)   返回date的月份值(1~12)
MONTHNAME(date)   返回date的月份名,如:SELECT MONTHNAME(CURRENT_DATE);
NOW()    返回当前的日期和时间
QUARTER(date)   返回date在一年中的季度(1~4),如SELECT QUARTER(CURRENT_DATE);
WEEK(date)   返回日期date为一年中第几周(0~53)
YEAR(date)   返回日期date的年份(1000~9999)

 

  1. 连接查询
  2. 内连接

 

  1. 语法:
  2.        SELECT … FROM join_table
  3.         [INNER] JOIN join_table2
  4.         [ON join_condition]
  5.         WHERE where_definition
 
  1. 只列出这些连接表中与连接条件相匹配的数据行。
  2. 外链接
  3. 交叉连接
  4. 自连接
 
  1. 正则匹配查询
索引:
1.创建索引:alter table 表名 add index 索引名(自己起)
2.删除索引:drop index 索引名 on  表名;
视图:
   1.创建视图:  create view 视图名 as  select 字段 from 表名
   2.删除视图:select *  from  视图名
   3.修改视图:update 视图名 set 字段=? where 字段=?;
 
1.MySQL中索引、主键和惟一性的区别是什么?
 
 索引  建立在一个或几个字段上,建立了索引后,表中的数据就按照索引的一定规则排列。这样可以提高查询速度
 主键  是表中数据的唯一标识。不同的记录的主键值不同。在建立主键时,系统会自动建立一个唯一性索引。
 唯一  性是建立在表中一个或者几个字段上。其目的是为了对于不同的记录,具有唯一性的字段的值是不同的。  
 
 
 
 
 
posted on 2017-02-24 14:21  国家欠我一个好哥哥  阅读(110)  评论(0)    收藏  举报