MySQL基础学习(-)终端基本操作
1.在Mac终端登陆 mysql -u root -p(我使用root用户登陆)
~ ⌚ 10:46:54
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.11 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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.查看当前所有的数据库 showt databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
2.打开指定的数据库名 use 库名
mysql> use sys
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
3.查看当前的库的所有表
show tables;
mysql> show tables;
+-----------------------------------------------+
| Tables_in_sys |
+-----------------------------------------------+
| host_summary |
| host_summary_by_file_io |
| host_summary_by_file_io_type |
| host_summary_by_stages |
| host_summary_by_statement_latency |
| host_summary_by_statement_type |
| innodb_buffer_stats_by_schema |
| innodb_buffer_stats_by_table |
| innodb_lock_waits |
| ....
.....
4.查看其他库的表 show tables from 库名;
mysql> show tables from mysql;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| component |
| db |
| default_roles |
| engine_cost |
| func |
| general_log |
| global_grants |
...
...
...
5.创建数据库create database 库名;
#创建一个男朋友的数据库,哈哈哈。。。
mysql> create database myBoyFriends;
Query OK, 1 row affected (0.52 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| myBoyFriends |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
6.创建表
create table 表名(
列名 列类型,
列名 列类型,
...
);
mysql> use myBoyFriends;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------------+
| Tables_in_myboyfriends |
+------------------------+
| bfInfo |
+------------------------+
1 row in set (0.00 sec)
mysql>
7.查看表结构 desc 表名
mysql> desc bfInfo;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| bf_name | varchar(20) | YES | | NULL | |
| bf_phone | int(12) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.12 sec)
mysql>
8.往表里插入一条数据
mysql> select * from bfInfo;
Empty set (0.00 sec)
mysql> insert into bfInfo values('keen',1234567);
Query OK, 1 row affected (0.12 sec)
mysql> select * from bfInfo;
+---------+----------+
| bf_name | bf_phone |
+---------+----------+
| keen | 1234567 |
+---------+----------+
1 row in set (0.00 sec)
7.查看mysql 服务器的版本
- 方式一:登陆mysql 服务端
- select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.11 |
+-----------+
1 row in set (0.00 sec)
退出mysql
mysql> quit
Bye
-
方式二:没有登陆到MySQL
- mysql --version;
$ mysql --version; mysql Ver 14.14 Distrib 5.7.22, for osx10.13 (x86_64) using EditLine wrapper
MySQL的语法规范
1.不区分大小写,但建议关键字大写,表名、列名小写
2.每条命令最好用分号结尾
3.每条命令根据需要,可以进行缩进 或换行
4.注释
单行注释:#注释文字
单行注释:-- 注释文字
多行注释:/* 注释文字 */
注:这是本人的学习笔记,如果有错误的地方希望大小伙伴们能指出,谢谢!
网友评论