您当前的位置: 首页 >  sql

嗨学编程

暂无认证

  • 1浏览

    0关注

    1405博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

MySQL常用技能篇

嗨学编程 发布时间:2019-06-18 16:17:59 ,浏览量:1

数据库操作

  创建数据库: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

 

数据导入导出

MySQL中如何数据及结构的导入导出来实现数据及结构的备份呢?在工作中使用到mysqldump命令。有兴趣可以看这里:http://blog.chinaunix.net/uid-26805356-id-4138986.html

  导入举例:

    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

关注
打赏
1663681728
查看更多评论
立即登录/注册

微信扫码登录

0.0539s