显示当前数据库

mysql> select database();
+------------+
| database() |
+------------+
| test |
+------------+
row in set (0.00 sec)

显示数据库表

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
row in set (0.00 sec)

mysql表复制

//复制表结构
mysql> create table t2 like t1;
Query OK, rows affected (0.03 sec)
mysql> select * from t1;
+------+
| id |
+------+
| |
| |
| |
| |
| |
| |
+------+
rows in set (0.01 sec)
//复制表数据 mysql> insert into t2 select * from t1;
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql> select * from t2;
+------+
| id |
+------+
| |
| |
| |
| |
| |
| |
+------+
rows in set (0.00 sec)

添加索引

//添加主键索引
mysql> alter table t1 add primary key(id);
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings:
//添加唯一索引
mysql> alter table t1 add column name varchar() not null; //给t1表添加一个name列
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings:
//查看表信息
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int() | NO | PRI | | |
| name | varchar() | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
rows in set (0.00 sec)
//清空表数据
mysql> truncate t1;
Query OK, rows affected (0.00 sec) mysql> select * from t1;
Empty set (0.00 sec)
//添加唯一索引
mysql> alter table t1 add unique index t1_name_unique(name);
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings:
//查看索引
mysql> show index from t1;
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1 | | PRIMARY | | id | A | | NULL | NULL | | BTREE | |
| t1 | | t1_name_unique | | name | A | | NULL | NULL | | BTREE | |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
rows in set (0.00 sec)
//添加普通索引
mysql> alter table t1 add column age int not null default ;
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings: mysql> desc t1
-> ;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int() | NO | PRI | | |
| name | varchar() | NO | UNI | NULL | |
| age | int() | NO | | | |
+-------+-------------+------+-----+---------+-------+
rows in set (0.00 sec) mysql> alter table t1 add index t1_in_age(age);
Query OK, rows affected (0.02 sec)
Records: Duplicates: Warnings: mysql> show index from t1;
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1 | | PRIMARY | | id | A | | NULL | NULL | | BTREE | |
| t1 | | t1_name_unique | | name | A | | NULL | NULL | | BTREE | |
| t1 | | t1_in_age | | age | A | NULL | NULL | NULL | | BTREE | |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
rows in set (0.00 sec)

删除索引

mysql> alter table t1 drop primary key;

mysql> show index from t1;
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| t1 | | t1_name_unique | | name | A | | NULL | NULL | | BTREE | |
| t1 | | t1_in_age | | age | A | NULL | NULL | NULL | | BTREE | |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
mysql> alter table t1 drop index t1_in_age;
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql> alter table t1 drop index t1_name_unique;
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings: mysql> show index from t1;
Empty set (0.00 sec)

设置字段自增长auto_increment

mysql> alter table t1 modify id int not null primary key auto_increment;
Query OK, rows affected (0.02 sec)
Records: Duplicates: Warnings: mysql> desc t1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int() | NO | PRI | NULL | auto_increment |
| name | varchar() | NO | | NULL | |
| age | int() | NO | | | |
+-------+-------------+------+-----+---------+----------------+
rows in set (0.00 sec)

批量插入数据

mysql> insert into t1(name,age) values("aaa",),("bbb",),("cc",),("abc",);
Query OK, rows affected (0.00 sec)
Records: Duplicates: Warnings: mysql> select * from t1;
+----+------+-----+
| id | name | age |
+----+------+-----+
| | aaa | |
| | bbb | |
| | cc | |
| | abc | |
+----+------+-----+
rows in set (0.00 sec)

备份数据

mysql> select name,age from t1 into outfile "/tmp/t1.txt";
ERROR (HY000): File '/tmp/t1.txt' already exists
mysql> select name,age from t1 into outfile "/tmp/t1.txt";
Query OK, rows affected (0.00 sec)
[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# ls
ssh-TkEopz2496  ssh-zMKSLp2473  t1.txt  test.sql

清空表数据

mysql> delete from t1;
Query OK, rows affected (0.00 sec)
mysql> select * from t1;
Empty set (0.00 sec)
//导入数据
mysql> load data infile '/tmp/t1.txt' into table t1(name,age);
Query OK, rows affected, warnings (0.00 sec)
Records: Deleted: Skipped: Warnings:
//清空表
mysql> truncate t1;
Query OK, rows affected (0.00 sec)
//两种清空表的方式在原理上不一样,我们可以看出delete方式的影响行数为32,而truncate则是0,那么也就是说delete是一行一行的删除的,
所以truncate在清楚数据上面比delete方式更高效,并且truncate会是auto_increment的值重置为1

重置auto_increment

mysql> delete from t1 where id > ;
Query OK, rows affected (0.00 sec) mysql> alter table t1 auto_increment=;
Query OK, rows affected (0.01 sec)
Records: Duplicates: Warnings:

load data方式导入数据,这种方式只是导入表数据而不会导入表结构,所以在单纯的数据导入上面更高效,我们可以看看导出文件的内容:

[root@localhost tmp]# cat t1.txt
aaa
bbb
cc
abc
aaa
bbb
cc
abc
aaa
bbb
cc
abc
aaa
bbb
cc
abc
aaa
bbb
cc
abc
aaa
bbb
cc
abc
aaa
bbb
cc
abc
aaa
bbb
cc
abc
mysql> load data infile '/tmp/t1.txt' into table t1(name,age);
Query OK, 32 rows affected, 64 warnings (0.00 sec)
Records: 32 Deleted: 0 Skipped: 0 Warnings:

case when语句

mysql> select id,name,age,case when age >=  then 'a' when age <= then 'b' else 'c' end as ddd from t1;
+----+------+-----+-----+
| id | name | age | ddd |
+----+------+-----+-----+
| | aaa | | c |
| | bbb | | a |
| | cc | | b |
| | abc | | c |
| | aaa | | c |
| | bbb | | a |
| | cc | | b |
| | abc | | c |
| | aaa | | c |
| | bbb | | a |
| | cc | | b |
| | abc | | c |
| | aaa | | c |
| | bbb | | a |
| | cc | | b |
| | abc | | c |
| | aaa | | c |
| | bbb | | a |
| | cc | | b |
| | abc | | c |
+----+------+-----+-----+
rows in set (0.00 sec)

常用函数:字符串函数

//字符串组合函数
mysql> select concat("hello","mysql") as title;
+------------+
| title |
+------------+
| hellomysql |
+------------+
row in set (0.00 sec) mysql> select concat("hello","mysql") as title;
+------------+
| title |
+------------+
| hellomysql |
+------------+
row in set (0.00 sec) mysql> select concat("hello","mysql","aaaa") as title;
+----------------+
| title |
+----------------+
| hellomysqlaaaa |
+----------------+
row in set (0.00 sec)
//字符串大小写转换
mysql> select lcase('HELLO MYSQL') as title;
+-------------+
| title |
+-------------+
| hello mysql |
+-------------+
row in set (0.00 sec) mysql> select ucase('hello mysql') as title;
+-------------+
| title |
+-------------+
| HELLO MYSQL |
+-------------+
row in set (0.00 sec)
//返回字符的长度
mysql> select length("hello mysql") as length;
+--------+
| length |
+--------+
| |
+--------+
row in set (0.00 sec)
//将字符重复N次
mysql> select repeat('hello mysql,',);
+--------------------------------------+
| repeat('hello mysql,',) |
+--------------------------------------+
| hello mysql,hello mysql,hello mysql, |
+--------------------------------------+
row in set (0.00 sec)
//替换字符串
mysql> select replace("hello mysql","mysql","php") as rp;
+-----------+
| rp |
+-----------+
| hello php |
+-----------+
row in set (0.00 sec)
//截取字符串,注意索引是从1开始
mysql> select substring("hello mysql",,) as sub;
+-------+
| sub |
+-------+
| hello |
+-------+
row in set (0.00 sec)
//返回字符在列表中的位置
mysql> select find_in_set("a","a,b,c,d");
+----------------------------+
| find_in_set("a","a,b,c,d") |
+----------------------------+
| |
+----------------------------+
row in set (0.00 sec)

常用函数:数学函数

//10进制转2进制
mysql> select bin();
+--------+
| bin() |
+--------+
| |
+--------+
row in set (0.00 sec)
//向上取整
mysql> select ceiling(1.2);
+--------------+
| ceiling(1.2) |
+--------------+
| |
+--------------+
row in set (0.00 sec)
//向下取整
mysql> select floor(1.2);
+------------+
| floor(1.2) |
+------------+
| |
+------------+
row in set (0.00 sec)
//获取最大值
mysql> select *,max(age) from t1 ;
+----+------+-----+----------+
| id | name | age | max(age) |
+----+------+-----+----------+
| | aaa | | |
+----+------+-----+----------+
row in set (0.00 sec)
//获取最小值
mysql> select *,min(age) from t1;
+----+------+-----+----------+
| id | name | age | min(age) |
+----+------+-----+----------+
| | aaa | | |
+----+------+-----+----------+
row in set (0.00 sec)
//获取一个0到1之间的随机数
mysql> select rand();
+-------------------+
| rand() |
+-------------------+
| 0.635864053513728 |
+-------------------+
row in set (0.00 sec)

常用函数:日期函数

//获取当前时间的日期部分
mysql> select curdate();
+------------+
| curdate() |
+------------+
| -- |
+------------+
row in set (0.00 sec)
//获取当前时间的小时部分
mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| :: |
+-----------+
row in set (0.00 sec)
//获取当前时间
mysql> select now();
+---------------------+
| now() |
+---------------------+
| -- :: |
+---------------------+
row in set (0.00 sec)
//mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| |
+------------------+
row in set (0.00 sec)
//获取当前时间戳
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| |
+------------------+
row in set (0.00 sec)
//时间戳转化为日期
mysql> select from_unixtime(unix_timestamp());
+---------------------------------+
| from_unixtime(unix_timestamp()) |
+---------------------------------+
| -- :: |
+---------------------------------+
row in set (0.00 sec) //获取时间中的年月日
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| |
+-------------+
row in set (0.00 sec)
mysql> select month(now());
+--------------+
| month(now()) |
+--------------+
| |
+--------------+
row in set (0.00 sec)
mysql> select day(now());
+------------+
| day(now()) |
+------------+
| |
+------------+
row in set (0.00 sec)

mysql基础操作整理(一)的更多相关文章

  1. MYSQL基础操作

    MYSQL基础操作 [TOC] 1.基本定义 1.1.关系型数据库系统 关系型数据库系统是建立在关系模型上的数据库系统 什么是关系模型呢? 1.数据结构可以规定,同类数据结构一致,就是一个二维的表格 ...

  2. MYSQL 基础操作

    1.MySQL基础操作 一:MySQL基础操作 1:MySQL表复制 复制表结构 + 复制表数据 create table t3 like t1; --创建一个和t1一样的表,用like(表结构也一样 ...

  3. 【MySQL】MySQL基础操作语句

    mysql基础操作语句,包括数据库的增.删.切换,以及表的增.删.改.查.复制. 创建数据库 mysql> create database tem; 使用数据库 mysql> use te ...

  4. MySQL基础操作&&常用的SQL技巧&&SQL语句优化

    基础操作     一:MySQL基础操作         1:MySQL表复制             复制表结构 + 复制表数据             create table t3 like t ...

  5. mysql数据库优化课程---13、mysql基础操作

    mysql数据库优化课程---13.mysql基础操作 一.总结 一句话总结:mysql复制表,索引,视图 1.mysql如何复制表? like select * 1.复制表结构 create tab ...

  6. MySQL基础操作(二)

    MySQL基础操作 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用.注意:使用视图时 ...

  7. 前端笔记之服务器&Ajax(中)MySQL基础操作&PHP操作数据库&Ajax

    一.数据库基础 1.1什么是数据库? 什么是数据库? 答:就是一个很大的一个文件,只不过这个文件可以通过一些‘命令’操作数据: 增.删.改.查数据: 数据库等于持久数据和数据操作的一个统称. 数据库是 ...

  8. PHP mysql基础操作

    mysql连接操作 //建立连接$con = mysql_connect('localhost', 'root', '123456');//判断是否连接成功if($con){ die('连接失败!'. ...

  9. 02 . Mysql基础操作及增删改查

    SQL简介 SQL(Structured Query Language 即结构化查询语言) SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发. SQL语句四大 ...

随机推荐

  1. HDU-1236 排名

    http://acm.hdu.edu.cn/showproblem.php?pid=1236 学会怎样按字典序排序的模板. 排名 Time Limit: 2000/1000 MS (Java/Othe ...

  2. Linux学习笔记7——linux中的静态库和动态库

    一.静态库的编译 静态库的编译过程如下: 1.编译成目标文件 这里有一个可选项-static,调用格式:gcc -c -static 代码文件名.c 2.归档成静态库 A.归档的工具是ar工具,使用a ...

  3. Matlab与DSP联合开发

    1.关于DSP开发环境 刚开始接触TI CCS的时候,用的是CCS2.2,当时CCS2.2又分成4个系列安装包 1.CCS6000 2.CCS5000 3.CCS2000 4.OMAP 都可以单独安装 ...

  4. offsetTop offsetLeft offsetWidth offsetHeight

    document // Html 的容器对象. document.documentElement //html 对象 document.body // body 对象 $(document.docum ...

  5. centos下apache thrift的安装

    参考:http://running.iteye.com/blog/1983463  thrift-0.9.0安装 最好切换到root用户操作,避免不必要的麻烦. 进行例子程序tutorial目录下,通 ...

  6. 去除 Visual Studio 中臃肿的 ipch 和 sdf 文件

    使用VS2010建立C++解决方案时,会生成SolutionName.sdf和一个叫做ipch的文件夹,这两个文件再加上*.pch等文件使得工程变得非常的庞大,一个简单的程序都会占用几十M的硬盘容量, ...

  7. crontab使用--linux下的定时任务程序

    crontab是一个linux下的定时运行程序,如果我们想让自己的程序定时执行, 可以把自己的程序交给这个程序来完成 第一步:配置crontab的脚本的默认编辑器,它的默认的编辑器不好用,我们配置vi ...

  8. JavaEE学习笔记---数据库操作篇

    测试JDBC和SQLServer的插入操作,源码如下: import java.sql.Connection;import java.sql.DriverManager;import java.sql ...

  9. Java多线程实现简单的售票程序

    设计一个多线程程序如下:设计一个火车售票模拟程序.假如火车站要有100张火车票要卖出,现在有5个售票点同时售票,用5个线程模拟这5个售票点的售票情况 1.要求打印出每个售票点所卖出的票号 2.各售票点 ...

  10. ng-select ng-options ng-repeat的用法与区别

    最近在用angularJS里的ng-select,ng-options,ng-repeat,发现有两点不太方便: 1.当数据复杂时,循环起来比较麻烦 2.首选项如果不设置,就会为空 整理一下它们的用法 ...