mysql

来源:互联网 发布:jquery给数组排序 编辑:程序博客网 时间:2024/05/20 21:45

时间函数

注意date, datetime, timestamp之间的区别

 

ADDTIME(原时间, 增加值) 在某个时间上增加一段时间

       select addtime('18:23:01', '01:01:01');

select addtime(now(),'3:0:0');

CURRENT_DATE()               当前日期

       select current_date();

CURRENT_TIME()                当前时间

       select current_time();

CURRENT_TIMESTAMP()          当前时间戳

       select current_timestamp();

DATE(时间)                                   返回制定时间的日期部分

       select date('2011-02-14 18:00:00');

DATE_ADD(日期,INTERVAL 增加值 类型)        在指定日期上对某个字段增加

       select date_add('2011-02-14 23:00:00',interval 10 month);

DATE_SUB(日期,INTERVAL 减少值 类型)         在指定日期上对某个字段减少

       select date_sub('2011-02-14 23:00:00',interval 1 year);

DATEDIFF(日期1, 日期2)         计算两个日期之间的差值

       select datediff('2000-02-14','2001-02-14');

NOW()         当前时间

       select now();

YEAR|MONTH|DATE|HOUR|MINUTE|SECOND(时间)           获取指定时间的某个字段

       select year('2011-02-14 23:00:00');

       select hour('2011-02-14 23:00:00');

1.1.     字符串函数

CHARSET(字符串)                                            返回字符串字符集

       select charset(name) from student;

CONCAT(字符串1[, 字符串2]... )                         连接字符串

       select concat('aaa', 'bbb', 'ccc');

INSTR(字符串, 子字符串)                                       查找子字符串出现位置, 注意序号从1开始

       select instr('abc', 'a');

UCASE(字符串)                                                        将字符串转为大写

       select ucase('aBc');

LCASE(字符串)                                                  将字符串转为小写

       select lcase('aBc');

LEFT(字符串, 长度)                                                 从字符串左边取指定长度个字符

       select left('aBc',2);

LENGTH(字符串)                                                     计算字符串长度

       select length('aBc');

REPLACE(字符串, 搜索字符串, 替换字符串)            将字符串中指定字符串替换为其他字符串

       select replace('abbcbbd', 'bb', 'ee');

STRCMP(字符串1, 字符串2)                                 逐个字符比较两个字符串, 如果是包含关系, 则返回长度差值

       select strcmp('abcc', 'abde');

       select strcmp('abc', 'ab');

SUBSTRING(字符串, 开始坐标[, 个数])              从字符串中截取

       select substring('abcdef', 3);

       select substring('abcdef', 3, 2);

LTRIM(字符串)                                                  去掉左边空白

       select ltrim('    abc  ');

       select concat('--', ltrim('    abc  '), '--');

RTRIM(字符串)                                                  去掉右边空白

       select concat('--', rtrim('    abc  '), '--');

TRIM(字符串)                                                     去掉左右两边空白

       select concat('--', trim('    abc  '), '--');

1.2.     数学函数

ABS(数字)                                      求绝对值

       select abs(10);

       select abs(-10);

BIN(十进制数)                              将十进制转换为二进制

       select bin(5);

HEX(十进制数)                             将十进制转换为十六进制

       select hex(10);

CONV(数字, 原进制, 目标进制)       转换进制

       select conv(12, 10, 16);

       select conv(12, 10, 2);

       select conv(12, 16, 2);

CEILING(小数)                             向上取整

       select ceiling(3.4);

FLOOR(小数)                                向下取整

       select floor(3.4);

ROUND(小数)                               四舍五入

       select round(3.4);

select round(3.5);

FORMAT(小数, 保留位数)                 保留小数位

       select format(3.1415926, 2);

LEAST(值,值[,值]...)                      取最小值

       select least(1,2,3,4);

       select least('a', 'b', 'c', 'd');

GREATEST(值,值[,值]...)                     取最大值

       select greatest(1,2,3,4);

       select greatest('a', 'b', 'c', 'd');

MOD(数字, 数字)                                取余

       select mod(3,2);

       select 3%2;

RAND()                                                 生成随机数, 14位小数, 0 <= n <= 1

       select rand();

2.       表的约束

约束的作用

限定某一列上的数据, 阻止非法数据录入, 提高程序健壮性.

2.1.     唯一约束 unique

unique约束的字段在整张表中唯一, 不可重复, 不包括多个NULL

 

创建表时设置唯一

create table test(

       id int,

       name varchar(20) unique

);

 

删除唯一约束

show create tabletest; 发现唯一索引名叫name

alter table testdrop index name;

添加唯一约束

alter table testchange name name varchar(20) unique;

2.2.     非空约束 not null

not null约束的字段不能为空

创建表时设置非空

create table test1(

       id int,

       name varchar(20) not null

);

删除非空约束

alter table test1change name name varchar(20);

添加非空约束

alter table test1change name name varchar(20) not null;

2.3.     主键约束 primary key通常我们在设计表的时候需要给每一条记录一个独有的标识, 我们就用主键来约束这个标识.

primary key用来标识一个字段, 这个字段是非空且唯一的.

 

创建表时设置主键

create tabletest2(

id int primary key,

name varchar(20)

);

 

删除主键

alter table test2drop primary key;

 

在制定列上添加主键

alter table test2change id id int primary key;

alter table test2add primary key(id);

 

设置主键自动增长

create table test3(

id int primary key auto_increment,

name varchar(20)

);

 

删除自增长

alter table test3change id id int;

 

设置自增长

alter table test3change id id int auto_increment;

 

UUID主键

128位的2进制, 32位16进制加上4个-

java.util.UUID.randomUUID().toString()

3c2372a4-da2a-4470-b17a-f2e50ac79636

外键约束foreign key

foreign key约束某一列的值是参照另外一列

 

创建表时添加外键约束

create table husband(

       id int primary key,

       name varchar(20) not null

);

create table wife(

       id int primary key,

       name varchar(20) not null,

       husband_id int,

       constraint husband_id_fk foreignkey(husband_id) references husband(id)

);

wife表的husband_id的值必须是husband表中的id

 

被外键引用的记录不能删除, 如果想要删除某条被引用的记录, 需要找到引用这条记录的记录, 解除关联

 

被外键引用的表不能删除, 如果想要删除被引用的表, 需要删除所有引用此表的外键

 

删除外键约束

alter table wifedrop foreign key husband_id_fk;

 

添加外键约束

alter table wife add constraint husband_id_fk foreignkey(husband_id) references husband(id)

 

一对多、多对一

 

多的一方设置外键

create tabledepartment(

       id int primary key auto_increment,

       name varchar(20)

);

 

create tableemployee(

       id int primary key auto_increment,

       name varchar(20),

       department_id int,

       constraint department_id_fk foreignkey(department_id) references department(id)

);

 

insert intodepartment(name) values('开发部');

insert intodepartment(name) values('市场部');

insert intoemployee(name, department_id) values('张三',1);

insert intoemployee(name, department_id) values('李四',1);

insert intoemployee(name, department_id) values('王五',2);

2.4.     一对一

 

独立外键, 没有依赖关系, 两个表的对象都可以独立存在.

create tablehusband(

       id int primary key auto_increment,

       name varchar(20)

);

 

create table wife(

       id int primary key auto_increment,

       name varchar(20),

       husband_id int,

       constraint husband_id_fk foreignkey(husband_id) references husband(id)

);

 

insert intohusband(name) values('张三');

insert intohusband(name) values('李四');

insert intowife(name, husband_id) values('冰冰',2);

insert intowife(name, husband_id) values('志玲',1);

主键即外键, 分为主表和从表, 从表依赖于主表, 从表中的对象不能单独存在.

注意从表的主键, 不能自动增长.

 

create tableperson (

       id int primary key auto_increment,

       name varchar(20)

);

 

create tableidcard (

       id int primary key,

       num varchar(20),

       constraint id_fk foreign key(id)references person(id)

);

 

insert intoperson(name) values('张三');

insert intoperson(name) values('李四');

insert intoidcard(id,num) values(2,'110123199009091234');

insert intoidcard(num,id) values('110123199009091234', 1);

2.5.     多对多

用一张关系表保存多对多的关系, 有两列分别引用两张表的主键,并且这两列组合起来成为联合主键

 

create table student(

       id int primary key auto_increment,

       name varchar(20)

);

 

create tableteacher (

       id int primary key auto_increment,

       name varchar(20)

);

 

create tablestudent_teacher(

       student_id int,

teacher_id int,

primary key(student_id, teacher_id),

constraint student_id_fk foreign key(student_id)references student(id),

constraint teacher_id_fk foreign key(teacher_id)references teacher(id)

);

 

insert into student(name)values('张三');

insert into student(name)values('李四');

insert into student(name)values('王五');

insert intoteacher(name) values('zxx');

insert intoteacher(name) values('lhm');

insert intostudent_teacher values(1,1);

insert intostudent_teacher values(2,1);

insert into student_teachervalues(2,2);

insert intostudent_teacher values(3,2);

3.       多表查询

3.1.     连接查询

当我们要插叙的数据不只是在一张表中, 我们就需要使用多表连接查询.

例如: 查询员工所在的部门名称,查询部门中员工名称, 都需要查询两张表.

 

注意:

在多表连接查询的时候, 如果没有有效的连接条件,所有表中的行会互相连接, 形成笛卡尔集.

为了避免笛卡尔集, 可以再where后加入有效的连接条件

3.2.     多表连接

多张表连接查询, 一张表外键引用另外一张表,另外一张表再引用其他表.

例如: 员工表引用部门, 部门表引用城市表. 这时如果想根据员工查城市, 或者根据城市查员工就需要将三张表连接查询

 

准备工作:

创建城市表, id主键自动生成, 带有名称

插入两条记录, 北京和上海

在部门表添加city_id, 外键引用城市表的id

将开发部的地址改为北京, 市场部的地址改为上海

 

练习:

查询所有员工, 员工所属部门以及部门所在城市

查询北京的所有员工

3.3.     自连接

自己和自己连接, 当前表的外键引用自己的主键.

例如: 员工的经理也是员工, 应该在员工表中添加一列经理id. 之后添加一个外键, 引用员工表的主键.

 

准备工作:

在员工表中添加manager_id, 外键引用员工表id

插入赵六, 孙七. 分别属于开发部和市场部.

将张三和李四的经理设置为赵六, 王五经理设置为孙七.

 

练习:

查询王五的经理姓名

查询赵六手下的员工名

 

原创粉丝点击