一、MySQL是什么?
MySQL数据库(Database)就是按照数据结构来组织,存储和管理数据的仓库 专业的数据库是专门对数据进行创建,访问,管理,搜索等操作的软件,比起我们自己用文件读写的方 式对象数据进行管理更加的方便,快速,安全.
(一)作用
对数据进行持久化的保存 方便数据的存储和查询,速度快,安全,方便 可以处理并发访问 更加安全的权限管理访问机制
二、mySQL的基本命令(重点!)
1.登录mysql,在终端输入以下命令,进行登录
(win10版)
C:\Users\Administrator>mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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.
(mac版)
MacBook-Pro:~ yc$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.28 Homebrew
Copyright (c) 2000, 2019, 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>
2.mysql的基础使用
1.查看当前mysql中所有的库。
库==>数据库==>就像文件夹一样,库里面可以存储很多个表
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
2.查看当前库中的所有数据表
mysql> show tables;
+--------------------+
| Tables_in_jianming |
+--------------------+
| users |
+--------------------+
1 row in set (0.00 sec)
mysql> select * from users;
Empty set (0.01 sec)
3.选择需要操作的库,打开库
mysql> use jianming
Database changed
mysql> show tables
-> show tables;
4.查看表中的数据.
mysql> select * from users;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 10 | 张三 | 25 |
+----+--------+------+
1 row in set (0.00 sec)
5.创建表
mysql> create table if not exists myusers(
-> id int not null primary key auto_increment,
-> name varchar(4) not null,
-> age tinyint,
-> sex enum('男','女')
-> )engine=innodb default charset=utf8mb4;
6.往表中添加数据
insert into users(id,name,age) values(12,'涨势',23),(15,'物理',18);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from users;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 10 | 张三 | 25 |
| 12 | 涨势 | 23 |
| 15 | 物理 | 18 |
+----+--------+------+
3 rows in set (0.00 sec)
总结
1.库和表的概念与关系
库就像是文件夹,库中可以有很多个表 表就像是我们的excel表格文件一样 每一个表中都可以存储很多数据 mysql中可以有很多不同的库,库中可以有很多不同的表 表中可以定义不同的列(字段), 表中可以根据结构去存储很多的数据