MySQL学习笔记-01-介绍、安装、数据库的创建、表的基本操作

文章目录

  • MySQL介绍
  • MySQL安装
    • 1. exe
    • 2. 免安装的方式
  • MySQL环境的验证
  • MySQL数据库的操作
    • 数据库的操作
    • 表的操作
      • 表的创建
      • 表的赋值
    • 数据的基本操作
      • 数据的插入
      • 删除数据
      • 修改数据
      • 数据查询
    • 运算符
      • 去除重复
      • 算数运算(+-*/)
      • 比较运算
      • 特殊的比较运算符

MySQL介绍

MySQL安装

1. exe

2. 免安装的方式

  • 解压压缩包

    mysql-5.7.18-winx64

    “D:\Program Files\mysql-5.7.18-winx64”

MySQL学习笔记-01-介绍、安装、数据库的创建、表的基本操作_第1张图片

  • 启动服务(不需要操作)
  • 注册成系统服务(开机启动)
    1. 管理员身份打开命令行
    2. 切换目录
C:\Windows\system32>d:

D:\>cd D:\Program Files\mysql-5.7.18-winx64\bin

D:\Program Files\mysql-5.7.18-winx64\bin>
  1. ​ 注册系统服务
C:\Windows\system32>d:

D:\>cd D:\Program Files\mysql-5.7.18-winx64\bin

D:\Program Files\mysql-5.7.18-winx64\bin>mysqld --install
Service successfully installed.

D:\Program Files\mysql-5.7.18-winx64\bin>
  • 验证系统服务是否安装成功

    window徽标+R 运行

MySQL学习笔记-01-介绍、安装、数据库的创建、表的基本操作_第2张图片

services.msc 

MySQL学习笔记-01-介绍、安装、数据库的创建、表的基本操作_第3张图片

  • 启动mysql服务

MySQL学习笔记-01-介绍、安装、数据库的创建、表的基本操作_第4张图片

MySQL环境的验证

打开cmd,切换到D:\Program Files\mysql-5.7.18-winx64\bin

  • 连接数据库
D:\Program Files\mysql-5.7.18-winx64\bin>mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecur

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>

图形界面的客户端操作

SqlYOG

  1. 注册

  2. 新建连接

MySQL学习笔记-01-介绍、安装、数据库的创建、表的基本操作_第5张图片
MySQL学习笔记-01-介绍、安装、数据库的创建、表的基本操作_第6张图片

MySQL数据库的操作

数据库的操作

  • 创建数据库
CREATE DATABASE imust

MySQL学习笔记-01-介绍、安装、数据库的创建、表的基本操作_第7张图片

  • 删除数据
DROP DATABASE imust_bak;

表的操作

表的创建

CREATE TABLE users(
  --  列的定义
  id        INT PRIMARY KEY AUTO_INCREMENT ,
  username  VARCHAR(50),
  PASSWORD  VARCHAR(50),
  last_login DATETIME,
  up_time    DATETIME,
  c_time     DATETIME
)

表的赋值

– 备份表
– 1 仅仅备份数据结构
– 给users表备份表结构到新表user_bak

CREATE TABLE user_bak  LIKE users ; 
CREATE TABLE trade_order_bak  LIKE trade_order ;

– 2 备份数据结构 +数据

CREATE TABLE trade_order_data SELECT * FROM trade_order

数据的基本操作

DML:数据插入、删除数据、更新数据

DQL:查询数据

数据的插入

-- 插入数据
INSERT INTO users
(username,PASSWORD,last_login,up_time,c_time)
 VALUES
 ('admin','123456','2022-02-21 14:58:01','2022-02-21 14:58:01','2022-02-21 14:58:01')

-- 忽略字段枚举
INSERT INTO users
(id,username,PASSWORD,last_login,up_time,c_time)
 VALUES
 (2,'root','abcdef','2022-02-21 14:58:01','2022-02-21 14:58:01','2022-02-21 14:58:01')

INSERT INTO users
 VALUES
 (3,'jshand','qwert','2022-02-21 14:58:01','2022-02-21 14:58:01','2022-02-21 14:58:01')

-- 一次插入多条数据
INSERT INTO users
(username,PASSWORD,last_login,up_time,c_time)
 VALUES
 ('acc01','qwert','2022-02-21 14:58:01','2022-02-21 14:58:01','2022-02-21 14:58:01'),
('acc02','qwert','2022-02-21 14:58:01','2022-02-21 14:58:01','2022-02-21 14:58:01'),
('acc03','qwert','2022-02-21 14:58:01','2022-02-21 14:58:01','2022-02-21 14:58:01'),
('acc04','qwert','2022-02-21 14:58:01','2022-02-21 14:58:01','2022-02-21 14:58:01');

删除数据

-- 删除的操作 [慎重操作]
-- 删除数据的时候一般情况下添加过滤条件
DELETE FROM users WHERE id = 5

修改数据

-- 修改数据
UPDATE 
  users 
SET
  PASSWORD = '987654321',
  up_time = '2022-02-21 15:09:50' 
WHERE id = 6 

数据查询

语法: select (column1,[column2,cloumn3]) from table

-- 查询sql
SELECT id ,username,PASSWORD,last_login,up_time,c_time FROM users

-- 开发的时候可以使用,生成代码不能用
SELECT * FROM users

运算符

查询的运算符, 去重。 distinct

去除重复

-- 去除重复
SELECT DISTINCT  NAME  FROM  address;

算数运算(±*/)

-- 查询所有订单id 和金额
SELECT order_id,amount	 FROM trade_order;

-- 查询订单8折的总金额
SELECT order_id,amount, amount*0.8 FROM trade_order;

SELECT order_id,amount, amount-10 FROM trade_order;

-- 列的别名
SELECT order_id,amount, amount/10 AS '除以10' FROM trade_order;

SELECT order_id,amount, amount/10 AS amount10 FROM trade_order;

SELECT order_id,amount, amount/10  amount10 FROM trade_order;

比较运算

=,!= ,<> ,>,<

-- 比较运算符
-- 查询订单总金额大于100的给与打折
SELECT order_id,amount, amount*0.8 FROM trade_order WHERE amount >100;
SELECT order_id,amount, amount*0.8 FROM trade_order WHERE amount <100;

SELECT order_id,amount, amount*0.8 FROM trade_order WHERE amount <=30;
SELECT order_id,amount, amount*0.8 FROM trade_order WHERE amount >=800;

SELECT order_id,amount, amount*0.8 FROM trade_order WHERE amount <>30;
SELECT order_id,amount, amount*0.8 FROM trade_order WHERE amount != 30;

特殊的比较运算符

  • 过滤区间 between and
-- 查询订单且 订单总金额在10--100
SELECT * FROM trade_order WHERE amount BETWEEN 10  AND  100;
-- 错误的写法  10应该放到前面
SELECT * FROM trade_order WHERE amount BETWEEN 100  AND  10;


SELECT * FROM trade_order WHERE o_time BETWEEN '2022-02-01 00:00:00'  AND  '2022-02-28 23:59:59';


SELECT * FROM address WHERE province BETWEEN '辽宁省' AND  '辽宁省1'

-- 尽量少用这个
SELECT * FROM address WHERE id = '2'

SELECT * FROM address WHERE id = 2

在写条件查询的时候尽量按照相同的数据类型编写SQL语句


  • 跟某一个值相似(不相似)
-- 查询所有 姓张的收货地址 
SELECT * FROM address;
-- % 模糊匹配0到多个字符 
SELECT * FROM address WHERE NAME LIKE '张%'
-- _有且仅有一个
SELECT * FROM address WHERE telphone LIKE '18404_75932'




CREATE TABLE `dept` (
  `deptno` INT(2) NOT NULL,
  `dname` VARCHAR(14) DEFAULT NULL,
  `loc` VARCHAR(13) DEFAULT NULL,
  PRIMARY KEY (`deptno`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;


INSERT  INTO `dept`(`deptno`,`dname`,`loc`) VALUES (10,'ACCOUNTING','NEW YORK'),(20,'RESEARCH','DALLAS'),(30,'SALES','CHICAGO'),(40,'OPERATIONS','BOSTON');


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` DECIMAL(7,2) DEFAULT NULL,
  `comm` DECIMAL(7,2) DEFAULT NULL,
  `deptno` INT(2) DEFAULT NULL,
  PRIMARY KEY (`empno`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;


INSERT  INTO `emp`(`empno`,`ename`,`job`,`mgr`,`hiredate`,`sal`,`comm`,`deptno`) VALUES (1,'dog','code',7369,'2018-06-05','5000.00','5000.00',10),(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),(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);



-- 查询名字里面有A所有员工
SELECT * FROM emp WHERE ename LIKE '%A%'  ;

-- 查询名字里面没有A所有员工
SELECT * FROM emp WHERE ename NOT  LIKE '%A%'  ;

  • 某一个值空的(非空)
-- 查询没有奖金的人员 NULL 代表空值,
-- null不和任何值相等,包括自身
-- null != 1 
-- null != '1'
-- null != null
SELECT * FROM emp WHERE comm IS NULL

SELECT * FROM emp WHERE comm  IS NOT  NULL
-- =0 是否包含 null
SELECT * FROM emp WHERE comm = 0


-- 查询奖金不为0  NULL不参与比较运算算 
SELECT * FROM emp WHERE comm <> 0

  • 在某些数值范围内的
-- 查询员工,部门编号是10和30
-- in 不走索引,无法提高查询效率
-- in最大个数是1000
SELECT * FROM emp WHERE deptno IN (10,30);

SELECT * FROM emp WHERE deptno NOT IN (10,30);

添加【指尖架构师】公众号,获取最新版面试题

你可能感兴趣的:(MySQL,学习,数据库,mysql)