前端需要了解的数据库有哪些

113 阅读8分钟

主题列表:什么是数据库, 何如链接数据库, 添加, 删除,修改, 查询,

theme: juejin highlight:30 ---数据库的定义: 数据持久化, 数据量较大 , 用户需求 数据库编程 (JDBC: Java Database Connection java连接数据库操作体系); ---数据库的类型: SQL Server: 中型数据库软件,微软 DB2,Oracle: 大型企业级数据库,收费(巨贵),不开源 MariaDB(Linux:centOS) , MySQL: "小型的",轻量级数据库,免费,开源(google) 初学者适合以MySQL作为切入点

在这里我们我们需要树立一个观念:服务器>数据库>表格>详细数据

下面以MySQL数据库为例: 安装:官网:www.mysql.com(可以下载MySQL最新版本); 如果要在任意目录中访问: mysql.exe , 就需要将文件安装路径路径配置在环境变量中;

1,连接数据库:MySQL文件夹子级bin目录下打开cmd,进入控制台 2,文件路径+sql\bin>mysql -u root -p 回车进入登陆界面

E:\sql\bin>mysql -u root -p Enter password: **** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 54 Server version: 5.7.17-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> 3,在上方输入密码,登录成功进入MySQL界面,如上述代码所示; 4;查看/添加/删除/数据库; 查看已有数据库;show mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hialian2 | | hualian | | mysql | | performance_schema | | sakila | | sys | | world | | xiaochenchen | +--------------------+ 9 rows in set (0.00 sec)

mysql> 创建新数据库:create mysql> CREATE DATABASE juejin; Query OK, 1 row affected (0.00 sec)

mysql> 删除数据库:drop mysql> drop database juejin; Query OK, 0 rows affected (0.13 sec)

mysql

5:查看/添加/删除/表格 01查看: 进入某一个数据库:use mysql> use hualian; Database changed mysql> 产看数据库中的表格:show mysql> show tables; +-------------------+ | Tables_in_hualian | +-------------------+ | tickets | | users | +-------------------+ 2 rows in set (0.00 sec)

mysql> 02:添加表格:create table 在这块i需要注意的是添加表格的时候需要跟上字段名称,就是表头结构名称,以及我们的条件; mysql> create table chen( -> id int primary key -> ); Query OK, 0 rows affected (0.04 sec) mysql> 在去查看我们的表格: mysql> desc chen; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec)

mysql> 注:里面的int 是我们的条件,primary key为主键 一个表格必须有一个主键及以上;想打关于我们的一个身份证一样,标识符,

03:删除表格;trop mysql> drop table chen; Query OK, 0 rows affected (0.01 sec)

mysql> 我们再打印数据库中的table 就没有chen这个表格了

6,表格中详细数据的添加/删除/修改/查询; 01:查询:select *from *号表示所有的内容,select也可以单独查询某一个字段名(我们每一步操作之后都可以查询一下看看发生了什么变化) mysql> select *from tickets; +----+------------+--------+---------------------+---------------------+ | id | name | isUsed | useExpire | getExpire | +----+------------+--------+---------------------+---------------------+ | 1 | 满100减10 | | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 2 | 满100减20 | | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 4 | 满100减30 |  | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 5 | 满两百件一 | | NULL | 2020-11-24 16:40:39 | +----+------------+--------+---------------------+---------------------+ 4 rows in set (0.01 sec)

mysql>

02:添加:insert into +表格名(字段名)(不写的话默认为全部) values(字段1的值,字段2的值) mysql> insert into tickets values(null,'小红',0,' 2020-12-31 23:59:59',' 2020-11-25 00:00:00'); Query OK, 1 row affected (0.01 sec) 结果: mysql> select * from tickets; +----+------------+--------+---------------------+---------------------+ | id | name | isUsed | useExpire | getExpire | +----+------------+--------+---------------------+---------------------+ | 1 | 满100减10 | | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 2 | 满100减20 | | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 4 | 满100减30 |  | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 5 | 满两百件一 | | NULL | 2020-11-24 16:40:39 | | 6 | 小红 | | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | +----+------------+--------+---------------------+---------------------+ 5 rows in set (0.01 sec)

mysql>

03,删除:delete 如果表名后不加where判断条件,那么删除整个表格 mysql> delete from tickets where id=6; Query OK, 1 row affected (0.00 sec)

mysql> select * from tickets; +----+------------+--------+---------------------+---------------------+ | id | name | isUsed | useExpire | getExpire | +----+------------+--------+---------------------+---------------------+ | 1 | 满100减10 | | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 2 | 满100减20 | | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 4 | 满100减30 |  | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 5 | 满两百件一 | | NULL | 2020-11-24 16:40:39 | +----+------------+--------+---------------------+---------------------+ 4 rows in set (0.00 sec)

mysql>

04;修改:update Update 表名 set 字段名=值[,字段名2=值2,字段3=值3…] [where 条件] mysql> update tickets set name=200 where id=1;//将id=1的字段中的name字段值改为200 Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from tickets; +----+------------+--------+---------------------+---------------------+ | id | name | isUsed | useExpire | getExpire | +----+------------+--------+---------------------+---------------------+ | 1 | 200 | | 2020-12-31 23:59:59 | 2020-11-25 13:22:34 | | 2 | 满100减20 | | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 4 | 满100减30 |  | 2020-12-31 23:59:59 | 2020-11-25 00:00:00 | | 5 | 满两百件一 | | NULL | 2020-11-24 16:40:39 | +----+------------+--------+---------------------+---------------------+ 4 rows in set (0.00 sec)

mysql>

以上就是MySQL数据库中简单的增删改查了; 备注一个注释方法: 单行: -- 杠杠空格 # 多行 /* */

关于数据库操作中引号的使用方法: 关于引号: 在sql操作中,遇到字符串/日期字符串的数值时,必须用单引号引起来 Delete from tableName where name=’’ 如果是非字符串类型的数据(int double) , 单引号加或者不加均可 Delete from tableName where sid = 1001 对的 Delete from tableName where sid = ‘1001’ 对的 insert into users values('7','fff','20'); 对的

结束再给大家推荐一个小插件:navicat 下载方式:大家可以上百度搜索 这个软件让整个MySQL数据库的控制台不得简介可视化起来; 使用方法: 使用查询工具->新建查询 ->sql命令 选中要执行的sql命令, 通过执行按钮或快捷键(ctrl+shift+r) 如果不选中,表示以全部(脚本)方式执行sql

重重重点: MySQL数据库内的查询; 1全字段查询:select * from +表格名:查询数据库所有的数据;

2显示全字段查询:将所有的字段名称写在select后面(显示全字段和全自动查询的区别在于,服务器会将号再转化为全字段名称,占有浏览器性能), ex:select id,username,pwd,age,phone,regDate,isVisible FROM 表格名;

3;部分字段查询 也叫投影查询: ex;select username,pwd FROM users;//查看username 和pwd字段

4;案例1:登录: 查询用户名为张三丰的用户信息 select id,username,age,phone from users WHERE username='张三丰'; 找到则会返回一条记录,找不到则返回空;

5;不等于的表示方法(查询用户名为张三丰之外的其他用户信息 select id,username,age,phone from users WHERE usern !='张三'; 也可以用:select id,username,age,phone from users WHERE username <>'张三' 上面两种方法得到的结果一样,只是写法略有不同;

6查询时间在多少时刻之前的数据: select id,username,age,phone from users WHERE time <'2020-1-1';

7,找出age字段不为空的数据: 错误示范1: select id,username,age,phone from users WHERE age!=null; 空的意思有选项未赋值;null必须要双引号括起来; 如果是赋值但为空白的字符串;用户为age !='';双引号中什么都不写 错误示范二: select id,username,age,phone from users WHERE age!='null'; 这里的用法加了引号虽然能获取数据,但实际上是一种取巧手段 正确用法: select id,username,age,phone from users WHERE age IS NOT NULL; 因为选项只有两个;为空和不为空;所以我们用is not 方法

8;and 且的用法: 01:查找姓名为张三丰的用户密码: select pwd from users where username ='张三丰'; 02:查找姓名为张三丰且密码为123的用户数据: select 'id' from users WHERE username =''张三丰' and pwd ='123';//返回一条数据 03:查找姓名为张三丰的用户密码,如果密码错误: select 'id' from users WHERE username =''张三丰' and pwd ='321'; //那么返回为空

9: or 或者的用法 表格中查找年龄部位null 或 部位隐藏状态的数据: select id,username,age,phone from users where age is not null or isvisible =1

10:between and 的用法: 在表格users中查找年龄在18-20之间的用户数据 01:<= >=的用法 select id,username,age,phone form users where age >= 18 and age <= 20 02:between and的写法 select id,username,age,phone from users where age between 18 and 20; 在表格users中找出所有在11月之内领用有效的优惠券信息(getexpire) select id,username,age,phone from users where getexpire where '2020-11-1 00:00:00' and '202011-30 23:59:59';

10:in的用法: 查询指定编号为1.3.5的数据: select * form tickets where id=1 or id-3 or id=5 in用法:select * from tickets where id in(1,3,5);

11:聚合函数的用法 01:统计:count(字段名) SELECT COUNT(id)FROM users#统计有几个id SELECT COUNT(id) 数据的数量 FROM users#统计有几个id SELECT COUNT(id) AS 数据的数量 FROM users#统计有几个id SELECT COUNT(*)FROM users#统计有几个id SELECT MAX(id) 最大的id FROM users;

SELECT MIN(id) 最小的id FROM users; SELECT AVG(pwd)求所有密码的和 FROM users; SELECT SUM(pwd)求所有密码的平均值 FROM users;