文章目录
数据库概述
-
数据库、数据库管理系统、SQL
-
数据库(Database,DB):是存储数据的仓库,本质上就是一堆文件
-
数据库管理系统(Database Management System,DBMS):用于管理数据库,对数据库中的数据进行增删改查
常见的数据库管理系统有:MySql、Oracle、MS SqlServer、DB2
-
SQL:结构化查询语言
程序员编写SQL语句,数据库管理系统执行SQL语句,对数据库中的数据进行增删改查操作
-
-
MySql是MySql AB公司开发的,MySql AB公司被SUN公司收购了,SUN公司又被Oracle公司收购了
Sql语句的分类
-
数据查询语言(DQL):凡是带有select关键字的都是查询语句
select查.
-
数据操作语言(DML):凡是对表中的数据进行增删改的都是DML
insert增
、delete删
、update改
-
数据定义语言(DDL):凡是对表结构进行增删改的都是DDL
create 增
、drop 删
、alter 改
-
事务控制语言(TCL)
commit 事务提交
、rollback 事务回滚
-
数据控制语言(DCL)
grant 授权
、revoke 撤销权限
MySql基本操作
服务的启动和终止
// 启动服务和终止服务一定要以管理员身份运行cmd
net start <服务项名称> // 启动服务
net stop <服务项名称> // 终止服务
C:\WINDOWS\system32>net start mysql // 启动MySql服务
The MySQL service is starting.
The MySQL service was started successfully.
C:\WINDOWS\system32>net stop mysql // 终止MySql服务
The MySQL service is stopping.
The MySQL service was stopped successfully.
登录MySql
// 使用bin目录下的mysql.exe命令来连接MySql服务器,因此一定要将bin目录配置到系统环境变量下
mysql -u<用户名> -p<密码> // 登录MySql(显示密码)
mysql -u<用户名> -p // 登录MySql(隐藏密码)
Enter Password: *****
// 登录MySql(显示密码)
C:\WINDOWS\system32>mysql -uroot -padmin // 本地登录(默认用户名为root)
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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(隐藏密码)
C:\WINDOWS\system32>mysql -uroot -p // 本地登录(默认用户名为root)
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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.
退出登录
exit或quit
mysql> exit
Bye
mysql> quit
Bye
查看有哪些数据库
show databases;
mysql> show databases; // 查看有哪些数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
创建数据库
create database <数据库名称>;
mysql> create database diamondk; // 创建一个名为diamondk的数据库
Query OK, 1 row affected (0.01 sec)
mysql> show databases; // 查看数据库列表,发现diamondk数据库已经成功创建
+--------------------+
| Database |
+--------------------+
| diamondk |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
删除数据库
drop database <数据库名称>;
mysql> drop database diamondk; // 删除diaondk数据库
Query OK, 0 rows affected (0.01 sec)
mysql> show databases; // 查看数据库列表,发现diamondk数据库已经成功删除
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
选择使用某个数据库
use <数据库名称>
mysql> use mysql // 选择使用mysql数据库
Database changed
查看数据库中有哪些表
use <数据库名称>; // 需要先选择使用某个数据库
show tables;
mysql> use mysql; // 选择使用mysql数据库
Database changed
mysql> show tables; // 查看该数据库下有哪些表
+------------------------------------------------------+
| Tables_in_mysql |
+------------------------------------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| password_history |
| plugin |
| procs_priv |
| proxies_priv |
| replication_asynchronous_connection_failover |
| replication_asynchronous_connection_failover_managed |
| replication_group_configuration_version |
| replication_group_member_actions |
| role_edges |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+------------------------------------------------------+
37 rows in set (0.01 sec)
-
表的理解
-
数据库中最基本的单元是表table
-
行(row):数据/记录
-
列(column):字段
每一个字段都有字段名、数据类型、约束等属性
-
-
导入数据
.sql这种文件被称为sql脚本文件。
sql脚本文件中编写了大量的sql语句。
我们执行sql脚本文件的时候,该文件中所有的sql语句会全部执行
通过以下方式执行脚本
source <.sql文件的文件路径> // 可以是绝对路径或相对路径,路径中不能有中文
mysql> create database bjpowernode; // 创建bjpowernode数据库
Query OK, 1 row affected (0.01 sec)
mysql> show databases; // 查看数据库列表,发现bjpowernode数据库已经创建成功
+--------------------+
| Database |
+--------------------+
| bjpowernode |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use bjpowernode; // 选择使用bjpowernode数据库
Database changed
mysql> show tables; // 展示bjpowernode数据库中的表
Empty set (0.00 sec) // 数据库为空
mysql> source bjpowernode.sql; // 导入当前目录下的bjpowernode.sql数据库文件
Query OK, 0 rows affected, 1 warning (0.01 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, 1 warning (0.02 sec)
Query OK, 0 rows affected, 5 warnings (0.02 sec)
Query OK, 0 rows affected (0.01 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
...
mysql> show tables; // 查看bjpowernode数据库的表,发现已经导入成功
+-----------------------+
| Tables_in_bjpowernode |
+-----------------------+
| dept | // 部门表
| emp | // 员工表
| salgrade | // 工资等级表
+-----------------------+
3 rows in set (0.00 sec)
导出数据
mysqldump <数据库名> > <.sql文件路径> -u<用户名> -p<密码>
mysqldump <数据库名> <表名> > <.sql文件路径> -u<用户名> -p<密码>
mysqldump bjpowernode >D:/bjpowernode.sql -uroot -padmin
C:\Windows\System32>mysqldump bjpowernode >D:/bjpowernode.sql -uroot -padmin
mysqldump: [Warning] Using a password on the command line interface can be insecure.
mysqldump bjpowernode emp >D:/bjpowernode_emp.sql -uroot -padmin
C:\Windows\System32>mysqldump bjpowernode emp >D:/bjpowernode_emp.sql -uroot -padmin
mysqldump: [Warning] Using a password on the command line interface can be insecure.
查看表中的数据
select * from <表名>;
mysql> select * from emp; //查看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 * from dept; //查看dept表中的所有数据
+--------+------------+----------+
| DEPTNO | DNAME | LOC |
+--------+------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+------------+----------+
4 rows in set (0.00 sec)
mysql> select * from salgrade; //查看salgrade表中的所有数据
+-------+-------+-------+
| GRADE | LOSAL | HISAL |
+-------+-------+-------+
| 1 | 700 | 1200 |
| 2 | 1201 | 1400 |
| 3 | 1401 | 2000 |
| 4 | 2001 | 3000 |
| 5 | 3001 | 9999 |
+-------+-------+-------+
5 rows in set (0.00 sec)
查看表结构
desc <表名>; // desc是describe的缩写,也可以使用describe <表名>;
mysql> desc emp; // 查看表emp的表结构
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| EMPNO | int | NO | PRI | NULL | | // 员工编号
| ENAME | varchar(10) | YES | | NULL | | // 员工姓名
| JOB | varchar(9) | YES | | NULL | | // 工作岗位
| MGR | int | YES | | NULL | | // 上级领导编号
| HIREDATE | date | YES | | NULL | | // 入职日期
| SAL | double(7,2) | YES | | NULL | | // 工资
| COMM | double(7,2) | YES | | NULL | | // 津贴、补助
| DEPTNO | int | YES | | NULL | | // 部门编号
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> desc dept; // 查看表dept的表结构
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| DEPTNO | int | NO | PRI | NULL | | // 部门编号
| DNAME | varchar(14) | YES | | NULL | | // 部门名称
| LOC | varchar(13) | YES | | NULL | | // 地理位置
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> desc salgrade; // 查看表salgrade的表结构
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| GRADE | int | YES | | NULL | | // 工资等级
| LOSAL | int | YES | | NULL | | // 最低工资
| HISAL | int | YES | | NULL | | // 最高工资
+-------+------+------+-----+---------+-------+
3 rows in set (0.00 sec)
查看版本号
select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.26 |
+-----------+
1 row in set (0.00 sec)
查看当前使用的是哪个数据库
select database();
mysql> select database();
+-------------+
| database() |
+-------------+
| bjpowernode |
+-------------+
1 row in set (0.00 sec)
Sql语句不区分大小写
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| bjpowernode |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> ShOw datAbasEs;
+--------------------+
| Database |
+--------------------+
| bjpowernode |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
Sql语句不见分号不执行
mysql> show
->
-> databases
->
-> ;
+--------------------+
| Database |
+--------------------+
| bjpowernode |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
终止当前语句的输入
mysql> show
->
->
->
-> \c // 输入\c后回车
mysql>
mysql> show
->
->
->
-> ^C // 按Ctrl+C
mysql>