博客
关于我
Mysql基本操作
阅读量:802 次
发布时间:2023-02-12

本文共 10617 字,大约阅读时间需要 35 分钟。

启动mysql

运行到安装目录C:\Program Files\MySQL\MySQL Server 5.6\bin>输入mysqld.exe –install得到结果Service successfully installed输入net start mysql  //启动mysqlnet stop mysql  //关闭mysql

 

用户成功登录

Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 8Server version: 5.6.10 MySQL Community Server (GPL)Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

数据库操作

  查看数据库的版本号和服务器当前时间

mysql> select version(),current_date;

输出:

+-----------+--------------+| version() | current_date |+-----------+--------------+| 5.6.10 | 2013-08-05 |+-----------+--------------+1 row in set (0.01 sec)

  查看服务器的所有数据库

mysql> show databases;
+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema |+--------------------+3 rows in set (0.00 sec)

  创建数据库

mysql> create database leaf;Query OK, 1 row affected (0.01 sec)
mysql> use leaf;Database changed
mysql> select database();+------------+| database() |+------------+| leaf       |+------------+1 row in set (0.00 sec)

  创建表

mysql> create table leaf(leaf_id varchar(6)    -> ,leaf_name varchar(10)    -> ,leaf_age int    -> ,leaf_sal int    -> ,leaf_bir date    -> ,leaf_sex varchar(5)    -> );Query OK, 0 rows affected (0.13 sec)

  显示当前数据库的表

mysql> show tables;+----------------+| Tables_in_leaf |+----------------+| leaf           |+----------------+1 row in set (0.00 sec)

  查看数据表的结构

mysql> describe leaf;+-----------+-------------+------+-----+---------+-------+| Field     | Type        | Null | Key | Default | Extra |+-----------+-------------+------+-----+---------+-------+| leaf_id   | varchar(6)  | YES  |     | NULL    |       || leaf_name | varchar(10) | YES  |     | NULL    |       || leaf_age  | int(11)     | YES  |     | NULL    |       || leaf_sal  | int(11)     | YES  |     | NULL    |       || leaf_bir  | date        | YES  |     | NULL    |       || leaf_sex  | varchar(5)  | YES  |     | NULL    |       |+-----------+-------------+------+-----+---------+-------+6 rows in set (0.01 sec)

  往表中插入数据

mysql> insert into leaf values    -> ('101','leaf','10','6000','2013-8-5','male'),    -> ('102','lea','20','5000','2013-8-4','male'),    -> ('103','le','30','4000','2013-8-3','female'),    -> ('104','l','35','4000','2013-8-2','female');Query OK, 4 rows affected, 2 warnings (0.01 sec)Records: 4  Duplicates: 0  Warnings: 2  

   查看表中数据

mysql> select * from leaf;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 101     | leaf      |       10 |     6000 | 2013-08-05 | male     || 102     | lea       |       20 |     5000 | 2013-08-04 | male     || 103     | le        |       30 |     4000 | 2013-08-03 | femal    || 104     | l         |       35 |     4000 | 2013-08-02 | femal    |+---------+-----------+----------+----------+------------+----------+4 rows in set (0.00 sec)

  修改数据

mysql> update leaf set leaf_id=100 where leaf_name='leaf';Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from leaf;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 100     | leaf      |       10 |     6000 | 2013-08-05 | male     || 102     | lea       |       20 |     5000 | 2013-08-04 | male     || 103     | le        |       30 |     4000 | 2013-08-03 | femal    || 104     | l         |       35 |     4000 | 2013-08-02 | femal    |+---------+-----------+----------+----------+------------+----------+4 rows in set (0.00 sec)

  对表中数据薪水项全部加2000

mysql> update leaf set leaf_sal=leaf_sal+2000;Query OK, 4 rows affected (0.00 sec)Rows matched: 4  Changed: 4  Warnings: 0mysql> select * from leaf;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 100     | leaf      |       10 |     8000 | 2013-08-05 | male     || 102     | lea       |       20 |     7000 | 2013-08-04 | male     || 103     | le        |       30 |     6000 | 2013-08-03 | femal    || 104     | l         |       35 |     6000 | 2013-08-02 | femal    |+---------+-----------+----------+----------+------------+----------+4 rows in set (0.00 sec)

  查询

mysql> select * from leaf where leaf_name='leaf';+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 100     | leaf      |       10 |     8000 | 2013-08-05 | male     |+---------+-----------+----------+----------+------------+----------+1 row in set (0.00 sec)

  查询表中,leaf_sal>6000的信息

mysql> select * from leaf where leaf_sal>6000;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 100     | leaf      |       10 |     8000 | 2013-08-05 | male     || 102     | lea       |       20 |     7000 | 2013-08-04 | male     |+---------+-----------+----------+----------+------------+----------+2 rows in set (0.00 sec)

  查询所有人工资情况

mysql> select leaf_name,leaf_sal from leaf;+-----------+----------+| leaf_name | leaf_sal |+-----------+----------+| leaf      |     8000 || lea       |     7000 || le        |     6000 || l         |     6000 |+-----------+----------+4 rows in set (0.00 sec)

  查询结果排序,按照工资低到高排序

mysql> select * from leaf order by leaf_sal;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 103     | le        |       30 |     6000 | 2013-08-03 | femal    || 104     | l         |       35 |     6000 | 2013-08-02 | femal    || 102     | lea       |       20 |     7000 | 2013-08-04 | male     || 100     | leaf      |       10 |     8000 | 2013-08-05 | male     |+---------+-----------+----------+----------+------------+----------+4 rows in set (0.00 sec)

   按照工资由高到低排序

mysql> select * from leaf order by leaf_sal desc;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 100     | leaf      |       10 |     8000 | 2013-08-05 | male     || 102     | lea       |       20 |     7000 | 2013-08-04 | male     || 103     | le        |       30 |     6000 | 2013-08-03 | femal    || 104     | l         |       35 |     6000 | 2013-08-02 | femal    |+---------+-----------+----------+----------+------------+----------+4 rows in set (0.00 sec)

  查看工资前两名

mysql> select * from leaf order by leaf_sal desc limit 2;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 100     | leaf      |       10 |     8000 | 2013-08-05 | male     || 102     | lea       |       20 |     7000 | 2013-08-04 | male     |+---------+-----------+----------+----------+------------+----------+2 rows in set (0.00 sec)

  随机查看两人

mysql> select * from leaf order by rand() limit 2;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 104     | l         |       35 |     6000 | 2013-08-02 | femal    || 103     | le        |       30 |     6000 | 2013-08-03 | femal    |+---------+-----------+----------+----------+------------+----------+2 rows in set (0.00 sec)

  查询8月份出生的人

mysql> select * from leaf where month(leaf_bir)=8;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 100     | leaf      |       10 |     8000 | 2013-08-05 | male     || 102     | lea       |       20 |     7000 | 2013-08-04 | male     || 103     | le        |       30 |     6000 | 2013-08-03 | femal    || 104     | l         |       35 |     6000 | 2013-08-02 | femal    |+---------+-----------+----------+----------+------------+----------+4 rows in set (0.00 sec)

  数据统计

mysql> select count(*) from leaf;+----------+| count(*) |+----------+|        4 |+----------+1 row in set (0.00 sec)

  使用统计函数

mysql> select    -> min(leaf_sal) as min_salary,    -> max(leaf_sal) as max_salary,    -> sum(leaf_sal) as sum_salary,    -> avg(leaf_sal) as avg_salary,    -> count(*) as employee_num    -> from leaf;+------------+------------+------------+------------+--------------+| min_salary | max_salary | sum_salary | avg_salary | employee_num |+------------+------------+------------+------------+--------------+|       6000 |       8000 |      27000 |  6750.0000 |            4 |+------------+------------+------------+------------+--------------+1 row in set (0.00 sec)

  表单数据删除

mysql> delete from leaf where leaf_name='l';Query OK, 1 row affected (0.06 sec)mysql> select * from leaf;+---------+-----------+----------+----------+------------+----------+| leaf_id | leaf_name | leaf_age | leaf_sal | leaf_bir   | leaf_sex |+---------+-----------+----------+----------+------------+----------+| 100     | leaf      |       10 |     8000 | 2013-08-05 | male     || 102     | lea       |       20 |     7000 | 2013-08-04 | male     || 103     | le        |       30 |     6000 | 2013-08-03 | femal    |+---------+-----------+----------+----------+------------+----------+3 rows in set (0.00 sec)

  数据表重命名

alter table 原始表名 rename as 新表名
mysql> alter table leaf rename as lea;Query OK, 0 rows affected (0.09 sec)

  删除数据表

mysql> drop database leaf;

 

  官方示例数据库

C:\Users\Administrator\Downloads\employees_db-full-1.0.6\employees_db>mysql -t -u root -p < employees.sql

 

 ====================================================================

Author by Leaf, Email: leaf816@gmail.com

====================================================================

转载于:https://www.cnblogs.com/lea-fu/p/3238079.html

你可能感兴趣的文章
NHibernate异常:No persister for的解决办法
查看>>
NIFI1.21.0_java.net.SocketException:_Too many open files 打开的文件太多_实际操作---大数据之Nifi工作笔记0051
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_日期类型_以及null数据同步处理补充---大数据之Nifi工作笔记0057
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_补充_更新时如果目标表中不存在记录就改为插入数据_Postgresql_Hbase也适用---大数据之Nifi工作笔记0059
查看>>
NIFI1.21.0_NIFI和hadoop蹦了_200G集群磁盘又满了_Jps看不到进程了_Unable to write in /tmp. Aborting----大数据之Nifi工作笔记0052
查看>>
NIFI1.21.0最新版本安装_连接phoenix_单机版_Https登录_什么都没改换了最新版本的NIFI可以连接了_气人_实现插入数据到Hbase_实际操作---大数据之Nifi工作笔记0050
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增删改数据分发及删除数据实时同步_通过分页解决变更记录过大问题_02----大数据之Nifi工作笔记0054
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_插入修改删除增量数据实时同步_通过分页解决变更记录过大问题_01----大数据之Nifi工作笔记0053
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表或全表增量同步_实现指定整库同步_或指定数据表同步配置_04---大数据之Nifi工作笔记0056
查看>>
NIFI1.23.2_最新版_性能优化通用_技巧积累_使用NIFI表达式过滤表_随时更新---大数据之Nifi工作笔记0063
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_根据binlog实现数据实时delete同步_实际操作04---大数据之Nifi工作笔记0043
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置binlog_使用处理器抓取binlog数据_实际操作01---大数据之Nifi工作笔记0040
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_实现数据插入数据到目标数据库_实际操作03---大数据之Nifi工作笔记0042
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_生成插入Sql语句_实际操作02---大数据之Nifi工作笔记0041
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_03_来吧用NIFI实现_数据分页获取功能---大数据之Nifi工作笔记0038
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_不带分页处理_01_QueryDatabaseTable获取数据_原0036---大数据之Nifi工作笔记0064
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_无分页功能_02_转换数据_分割数据_提取JSON数据_替换拼接SQL_添加分页---大数据之Nifi工作笔记0037
查看>>
NIFI从PostGresql中离线读取数据再导入到MySql中_带有数据分页获取功能_不带分页不能用_NIFI资料太少了---大数据之Nifi工作笔记0039
查看>>
nifi使用过程-常见问题-以及入门总结---大数据之Nifi工作笔记0012
查看>>
NIFI分页获取Mysql数据_导入到Hbase中_并可通过phoenix客户端查询_含金量很高的一篇_搞了好久_实际操作05---大数据之Nifi工作笔记0045
查看>>