MySQL01(入门篇)

MySQL入门-1

这里主要讲简单mysql命令行和简单查询语句

登陆MySQL
mysql -uroot -p密码
C:\Users\Administrator>mysql -uroot -p******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
完美卸载Mysql
  • 双击安装包,点击remove
  • 手动删除program Files里面的Mysql
  • 手动删除programData(这个是隐藏的文件,点击查看,打上显示隐藏的对勾,就可以看见)里面的Mysql
sql,DB,DBMS分别是什么,他们之间的关系是什么?

DB:

​ DataBase(数据库,数据库实际上在硬盘上以文件的形式存在)

DBMS:

​ DataBase Management System(数据库管理系统,常见的有:MySQL Oracle DB2 Sybase SqServer…)

SQL:

​ 结构化查询语言,是一门标准通用的语言。标准的sql适合于所有的数据库产品

​ SQL在执行的时候,实际上内部也会先进行编译,然后再执行sql(sql语句的编译由DBMS完成)

DBMS负责执行sql语句,通过执行sql语句来操作DB当中的数据
DBMS-(执行)->SQL-(操作)->DB
表的理解:tabel

table是数据库的基本组成单元,所有的数据都以表格形式组织,目的是可读性强

一个表包括行和列:

​ 行:被称为数据/记录(data)

​ 列:被称为字段(column)

每一个字段应该包括那些属性?

​ 字段名,数据类型,相关约束

学习MySQL主要是学习SQL语句

  • DQL(数据查询语言):查询语句,凡是select都是DQL。
  • DML(数据操作语言):insert delete ,updata ,对表当中的数据进行增删改。
  • DDL(数据定义语言): create drop alter 对表结构的增删改。
  • TCL(事物控制语言):commit提交事务,rollback回滚事务(TCL中T是Transaction)。
  • DCL(数据控制语言): grant授权,revoke撤销权限等。

MySQL命令

1.查看数据库
>show databases;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| atguigu            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
2.创建数据库
>create database 数据库名;(它属于Mysql命令,不是SQL语句)
mysql> create database bjpowernode;
Query OK, 1 row affected (0.02 sec)
3.使用数据库
>use 数据库名;(它属于Mysql命令,不是SQL语句)
mysql> use bjpowernode;
Database changed
4.查看数据库的表
>show tables;(它属于Mysql命令,不是SQL语句)
mysql> show tables;
Empty set (0.01 sec)
5.初始化数据
>source 文件路径

mysql> source D:\前端学习\bjpowernode.sql
Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)
...
6.再次查看表的列表
mysql> show tables
    -> ;
+-----------------------+
| Tables_in_bjpowernode |
+-----------------------+
| dept                  |
| emp                   |
| salgrade              |
+-----------------------+
3 rows in set (0.00 sec)
7.删除数据库
drop database 数据库名
mysql> drop database bjpowernode;
Query OK, 3 rows affected (0.02 sec)
8.查看表结构
>desc 表名称
mysql> desc emp;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| EMPNO    | int(4)      | NO   | PRI | NULL    |       |
| ENAME    | varchar(10) | YES  |     | NULL    |       |
| JOB      | varchar(9)  | YES  |     | NULL    |       |
| MGR      | int(4)      | YES  |     | NULL    |       |
| HIREDATE | date        | YES  |     | NULL    |       |
| SAL      | double(7,2) | YES  |     | NULL    |       |
| COMM     | double(7,2) | YES  |     | NULL    |       |
| DEPTNO   | int(2)      | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)

9.查看表数据
>select * from 表名称;
mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME  | JOB       | MGR  | HIREDATE   | SAL     | COMM    | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800.00 |    NULL |     20 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600.00 |  300.00 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250.00 |  500.00 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975.00 |    NULL |     20 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250.00 | 1400.00 |     30 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850.00 |    NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450.00 |    NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-04-19 | 3000.00 |    NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000.00 |    NULL |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500.00 |    0.00 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-05-23 | 1100.00 |    NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950.00 |    NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000.00 |    NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1982-01-23 | 1300.00 |    NULL |     10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)

mysql常用命令

查询当前使用的数据库
select database();
mysql> select database();
+-------------+
| database()  |
+-------------+
| bjpowernode |
+-------------+
1 row in set (0.00 sec)

查询当前版本号

select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.15    |
+-----------+
1 row in set (0.00 sec)

结束一条语句
\c
mysql> select
    ->
    ->
    ->
    -> \c
mysql>

退出mysql
exit
查看创建表的语句
>show create table 表名字
mysql> show create table emp;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                                        |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| emp   | CREATE TABLE `emp` (
  `EMPNO` int(4) NOT NULL,
  `ENAME` varchar(10) DEFAULT NULL,
  `JOB` varchar(9) DEFAULT NULL,
  `MGR` int(4) DEFAULT NULL,
  `HIREDATE` date DEFAULT NULL,
  `SAL` double(7,2) DEFAULT NULL,
  `COMM` double(7,2) DEFAULT NULL,
  `DEPTNO` int(2) DEFAULT NULL,
  PRIMARY KEY (`EMPNO`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

简单查询语句

select 字段名1,字段名2,字段名... from 表名;

tip:sql语句不区分大小写,每一条sql语句以分号结尾

mysql> select
    ->          ENAME
    -> from
    ->          emp;
+--------+
| ENAME  |
+--------+
| SMITH  |
| ALLEN  |
| WARD   |
| JONES  |
| MARTIN |
| BLAKE  |
| CLARK  |
| SCOTT  |
| KING   |
| TURNER |
| ADAMS  |
| JAMES  |
| FORD   |
| MILLER |
+--------+
14 rows in set (0.00 sec)
案例 查询员工年薪
mysql> select
    ->          ename,sal*12 as '年薪'
    -> from
    ->          emp;
+--------+----------+
| ename  | 年薪        |
+--------+----------+
| SMITH  |  9600.00 |
| ALLEN  | 19200.00 |
| WARD   | 15000.00 |
| JONES  | 35700.00 |
| MARTIN | 15000.00 |
| BLAKE  | 34200.00 |
| CLARK  | 29400.00 |
| SCOTT  | 36000.00 |
| KING   | 60000.00 |
| TURNER | 18000.00 |
| ADAMS  | 13200.00 |
| JAMES  | 11400.00 |
| FORD   | 36000.00 |
| MILLER | 15600.00 |
+--------+----------+
14 rows in set (0.00 sec)

注意:

sql语句中支持数学运算

别名

as 后面接的是给查询的字段起的别名

字符串

标准的sql语句中要求字符串使用单引号括起来,虽然mysql支持双引号

按条件查询

select 字段,字段,.. from 表名 where 查询条件

tip:执行顺序:先from,然后where ,最后select

符号说明
“>”大于
“<”小于
“>=” “<=”大于或等于 小于或等于
“<>”或“!=”不等于
and相当于&&
between… and…相当于<= and >= ,也可以用在字符串方面
is null | is not null为空或者不为空,在数据库中Null不是一个值,代表什么也没有,为空。空不是一个值,能用用等号衡量
or或者
in包含,相当于多个or(not in 表示不在这个范围中)
like(类似正则表达式)like称为模糊查询,支持%或下划线匹配 , %表示任意多个字符,下划线表示任意一个字符,如果包含%_ 则必须转义 ‘"%"’ _
案例 : 查询工资等于3000的员工
mysql> select
    ->          ename
    -> from
    ->          emp
    ->
    -> where
    ->          sal=3000;
+-------+
| ename |
+-------+
| SCOTT |
| FORD  |
+-------+
2 rows in set (0.00 sec)

案例 : 查询工资不等于3000的员工

mysql> select
    ->          ename
    -> from
    ->          emp
    ->  where
    ->          sal<>3000;
+--------+
| ename  |
+--------+
| SMITH  |
| ALLEN  |
| WARD   |
| JONES  |
| MARTIN |
| BLAKE  |
| CLARK  |
| KING   |
| TURNER |
| ADAMS  |
| JAMES  |
| MILLER |
+--------+
案例 : 查询工资在1000到3000包含1000,3000的员工
mysql> select ename,sal from emp where sal between 1000 and 3000;
+--------+---------+
| ename  | sal     |
+--------+---------+
| ALLEN  | 1600.00 |
| WARD   | 1250.00 |
| JONES  | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE  | 2850.00 |
| CLARK  | 2450.00 |
| SCOTT  | 3000.00 |
| TURNER | 1500.00 |
| ADAMS  | 1100.00 |
| FORD   | 3000.00 |
| MILLER | 1300.00 |
+--------+---------+
11 rows in set (0.00 sec)

案例 : 查询津贴值为空的员工
mysql> select
    ->          ename,comm
    -> from
    ->          emp
    -> where
    ->          comm is null;
+--------+------+
| ename  | comm |
+--------+------+
| SMITH  | NULL |
| JONES  | NULL |
| BLAKE  | NULL |
| CLARK  | NULL |
| SCOTT  | NULL |
| KING   | NULL |
| ADAMS  | NULL |
| JAMES  | NULL |
| FORD   | NULL |
| MILLER | NULL |
+--------+------+
10 rows in set (0.00 sec)
案例:查询工资是5000和工资是800的员工
mysql> select
    ->          ename,sal
    -> from
    ->          emp
    -> where
    ->          sal in (800,5000);
+-------+---------+
| ename | sal     |
+-------+---------+
| SMITH |  800.00 |
| KING  | 5000.00 |
+-------+---------+
2 rows in set (0.00 sec)

tip: in (800,5000)不是800到5000之间,而是800or5000

查询名字中含有c的员工
mysql> select ename from emp where ename like '%c%';
+-------+
| ename |
+-------+
| CLARK |
| SCOTT |
+-------+
2 rows in set (0.00 sec)
查询名字中第二个字符是o的员工
mysql> select ename from emp where  ename like '_o%';
+-------+
| ename |
+-------+
| JONES |
| FORD  |
+-------+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值