写在之前的话:
之前一直在用MSSERVER,刚用MySQL时有很多的不适应。就此小结一下工作中遇到的问题和场景(用的不是很深入,供初学者参考),文中出现的局限性欢迎指出
MySQL有客户端式(SQLyog),可托拉拽和写代码;或者通过命令行的方式进行交互(mysql -h10.***.***.*** -P*** -u*** -p****);(170920补充:hive的语法最接近MySQL)
MySQL作为开源的数据库,在企业应用中十分普遍。
数据库操作
创建数据库:create database demon;
删除数据库:drop database demon;
表操作
创建
1.1、创建数据表
create table s_position
(
id int not null auto_increment,
name varchar(20) not null default '经理', #设定默认值
description varchar(100),
primary key PK_positon (id), #设定主键
unique (id,name) #设定唯一值
);
1.2、复制表/结构(若仅复制结构使用limit 0 选项)
create table tab1 as select * from table1;
1.3、创建临时表(临时表只在当前连接可见,当这个连接关闭的时候,会自动drop)
create temporary table tab1 (name varchar(25),...);
1.4、创建表进行判断
create table if not exists tabl1 (.....);
1.5、表重命名
alter talbe table1 rename as table2;
修改
2.1、增加列
alter talbe table1 add (test vhar(10),hehe char(12));
2.2、修改列
alter table table1 modify test char(20) not null
2.3、修改默认值
alter table table1 alter test set default 'system';
2.4、去掉默认值
alter table table1 alter test drop default;
2.5、去掉列
alter table table1 drop column test;
删除多列
alter table table1 drop column test1,
column test2,...;
2.6、创建索引
create index index_name on table_name(column_name);
2.7、删除主键
alter table table1 drop primary key;
2.8、增加主键
alter table table1 add primary key PK_depart_pos (department_id,position_id);
更新
格式
单表:update [LOW_PROORITY] [IGNORE] TABLENAME set COLUMN_NAME="***" WHERE CLOLUMN_NAME="&&&" [ORDER BY ...] [LIMIT rows]
或
多表:update [LOW_PROORITY] [IGNORE] TABLENAME [,tab2,tab3,....] set col_name1=expr1,.... [where ...]
说明
如果指定关键词LOW_PRIORITY,update的执行将被延迟,直到没有其他的客户端正在读取表;如果指定关键词IGNORE,则更新语句将不会异常中止,即使在更新过程中出现了重复键错误。导致冲突的记录将不会被更新;如果在一个表达式中从tbl_name中访问一个列,update使用咧的当前值
示例:多表更新
update tab1 ,tab2 set column1=column2,... where condition1='***'
常用函数
1、date和time
a.当前datetime:now():2017-06-07 14:31:52
b.返回时间:time():select time(now()); --->14:31:52
b1.返回日期:date():select date(now()); --->2017-06-07
c.返回年月日:year()/month()/day()
d.返回时分秒:hour()/minute()/second()
e.取时间含义:dayname()--星期几;
dayofweek():返回日期对应的星期:1--周日;2--周一;。。
dayofyear():返回日期为该年的第多少天;
last_day():返回某个日期的最后一天;-->select last_day(now()); '2017-06-30'
f.日期计算: interval:灵活性加减日期
select now()+interval +5 day;
select now()+interval -4 minute;
datediff(enddate,start_date),返回天数
timestampdiff(interval,begin_date,end_date ):求对应的时间差(天、时、分、秒)
2、字符串函数
a.长度
char_length():字符串长度 我是 --> 2
length():字符长度,”我是” --> 4 【字母/符号/数字 一个字符】
b.拼接函数
concat(str1,str2,...):
concat(seperator,str1,str2,...):带有分隔符的拼接函数
c.字符查找
locate(find_str,str,start):
d.字符截取
substring(str,start[,end])
e.替换
replace(str,old_str,new_str)
f.插入
insert(str,start,length,repr):在指定的位置插入字符或字符串
示例:select insert('aaabcdefg',3,5,'what') ---aawhatfg
select insert('aaabcdefg',3,1000,'what') ---aawhat
g.补充:
补充:①重复函数repeat(str,count)
②大小写转化:lower(str)/Lcase(str)、Upper(str)/Ucase(str)
③反转函数:reverse(str)
④空格函数:space(N)
⑤去空格函数:ltrim()、rtrim()、trim()
3.数值函数
a.转换
cast
b.截取
round(data,n):四舍五入,保留小数位数n位
truncate(data,n):截取数据到n位,不四舍五入
floor():数据的最大整数部分
ceil():大于数据的最小整数
c.取最小值
least(arg1,...):取最小值
d.数据处理
rand():随机小数
format(,n):数据格式化,转化为含千分位数据,四舍五入保留n位小数
4.控制流函数
a.case
case when ....then ...else ... end
b.if
if(condition,true_value,false_value)
c.ifnull
ifnull(arg1,arg2):arg1位空时则取值arg2,
d.nullif
nullif(arg1,arg2):arg1=arg2时为空,否则为arg1
数据导入导出(20171011更)
导入举例:
load data local infile '/shj/etl/new_data.csv' into table bi.test(a,b,c,d);
导出举例(这里我们通过-d参数,只导出数据结构;--skip-lock-tables,避免出现权限不足的1044错误):
mysqldump -h** -P3306 -u** -p** -d --skip-lock-tables ecf_webapp acct_business_account acct_loan acct_payment_log >~/sunhuajian/etl/ecf_webapp_structure.sql
转载请注明出处!欢迎邮件沟通:shj8319@sina.com
- mysql常用技能分享
一,MySQL查询的五种子句: 1,where(条件查询),常用的运算符: ①比较运算符 > , < , = , != , >= , <= , in( ) , between ...
- Mysql 常用 SQL 语句集锦
Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...
- Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)
Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...
- MySQL之MySQL常用的函数方法
MySQL常用函数 本篇主要总结了一些在使用MySQL数据库中常用的函数,本篇大部分都是以实例作为讲解,如果有什么建议或者意见欢迎前来打扰. limit Select * from table ord ...
- MySQL 常用语句 (汇集)
原文地址:MySql常用语句作者:wuyanle 一.mysql常用语句 创建,删除和最基本查询: 显示数据库 mysql->show databases; 创建数据库 mysql-> ...
- MYSQL常用命令集合(转载)
文章出处:http://www.cnblogs.com/q1ng/p/4474501.html 1.导出整个数据库mysqldump -u 用户名 -p --default-character-set ...
- MYSQL常用命令集合
1.导出整个数据库 mysqldump -u 用户名 -p --default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin1) mysqld ...
- IDEA第五章----Git常用技能
前几篇已经介绍了idea的环境搭建及基础配置常用模板等,这一章我们介绍下idea中git的一些常用技能,包括提交文件,排除提交文件,合并分支,解决冲突,还原代码等等等. 第一节:Git常用技能 Git ...
- 学习mysql语法--基础篇(一)
前 言 mysql mysql语法--本篇学习都是通过使用Navicat Premium(数据库管理工具),连接mysql数据. 本篇学习主要有两个部分: 一.创建用户,创建数据库,给 ...
随机推荐
- nginx高级-前端必会
需要设置的几个参数: 基本配置文件 user www www; worker_processes auto; error_log /www/wwwlogs/nginx_error.log crit; ...
- Unity脚本生命周期 图解
简单总结的话就是: Awake():初始化时执行,类似c#中的构造函数 OnEnable() Start() FixUpdate() Update() OnDisable() OnDestory() ...
- mybatis中if及concat函数的使用
- Eclipse中使用GIT将文件还原至上一版本
GIT将文件还原至上一版本: 选中文件——右击——Replace With——HEAD Revision:
- CF 949C Data Center Maintenance_强联通分量_思维题
题意: 某土豪公司建立了n个数据中心,把m份资料每份在其中的两个数据中心备份. 每个数据中心在一天h个小时当中有一个小时需要维护,此时不提供资料下载服务. 现在土豪公司想要将其中若干个数据中心的维护时 ...
- Hadoop HA 与 Federation
最近在做Hadoop上应用开发,需要和HA集成,active name node 切换不能影响应用的运行.在研究HA背景的同时,发现HA和Federation 配置中共用了nameservices 的 ...
- WordCount合作--自己部分
前言: (1)合作者:201631062127,201631062625 (2)合作代码地址:WordCount 一.结对的PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟 ...
- POJ——T2421 Constructing Roads
http://poj.org/problem?id=2421 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24132 ...
- POJ 2888
思路挺清晰的.不过,我就是WA.不清楚为什么,很多数据都过了. 其实,一个置换后若有循环节个数为K,则N必定可以除以尽K.而K正好可以看成一个环.为什么呢?看前K个珠子,就是一个环,而后面的若干个K个 ...
- node12---mongodb
一.传统数据库技术回顾 数据库就是存储数据的,那么存储数据就用txt就行了啊,为什么要有数据库? 理由之1: 数据库有行.列的概念,数据有关系,数据不是散的. 老牌数据库,比如MySQL.SQL Se ...