学习目标:
1.一个月学会MySQL的基本操作
2.完成第1个目标
Mac MySQL的基本操作(命令):
1、 连接MySQL数据库
首先打开终端,输入:
/usr/local/MySQL/bin/mysql -u root -p
会出现输入密码的界面:输入密码(不考虑密码忘了的情况)
Enter password:
出现下面代码,MySQL连接成功
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 8.0.22 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.
mysql>
2、 查看MySQL有哪些数据库
输入:记住分号是英文!!
mysql> show databases;
显示如下:默认有四个数据库(Logistics 是我自己的数据库,不用管)
+--------------------+
| Database |
+--------------------+
| information_schema |
| Logistics |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
3、 使用某一个数据库
假设使用mysql数据库
mysql> use mysql;
显示:
Database changed
mysql>
4、 创建一个数据库
创建一个叫pjpowernote的数据库
mysql> create database bjpowernote;
显示:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| bjpowernote |
| information_schema |
| Logistics |
| mysql |
| performance_schema |
| sys |
+--------------------+
5、 查看数据库下有哪些表
提示:数据库是由一个一个表组成的,表用来存储数据。
mysql> show tables;
显示:(此处显示的是mysql数据库中的表,自带的)
+----------------------------------------------+
| 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 |
| 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 |
+----------------------------------------------+
34 rows in set (0.00 sec)
mysql>
6、 SQL语句的分类
(1)数据定义语言(DDL):主要针对的是表的结构
create:新建;
drop:删除;
alter:修改;
(2)数据操作语言(DML):主要针对的是表的数据
select:查询;
insert:增;
delete:删;
update:改;
(3)事务控制语言(TCL):针对事务
commit:事务提交;
rollback:事务回滚;
(4)数据控制语言(DCL):
grant:授权权限;
revoke:撤销权限;
7、 怎样导入现有的数据库
提前准备好了一份练习数据库:bjpowernote.sql 输入:(直接在source后面把数据库找到拖进去就行!注意路径不能是中文)
1.进入bjpowernote数据库:
mysql> use bjpowernote
Database changed
2.导入(拖上去):
mysql> source ------------------;
显示一下:
mysql> show tables;
+-----------------------+
| Tables_in_bjpowernote |
+-----------------------+
| DEPT |
| EMP |
| SALGRADE |
+-----------------------+
3 rows in set (0.00 sec)
就这样导入成功啦!!!