Ubuntu MySQL学习总结(一)

在折腾了两天后,终于开始学习的MySQL这个关系型数据库的基本功能和语句了。

来来来,不要废话直接开始吧!

首先进入数据库:mysql -u root -p回车后输入之前自己设置好的密码即可看见欢迎说明你成功进入数据库!

chujun@chujun:~$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-0ubuntu0.18.10.1 (Ubuntu)

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> 

接下来就是看看安装的数据库里有什么表格了:

show databases;     这里需要注意不要漏掉分号哦!

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.04 sec)

mysql> 

然后就是新建数据库的命令create database mysql_learn; 这里的mysql_learn是你的数据库名称,根据你自己的实际需要取就好,还是不要漏掉分号!切记切记!然后再查看一次,已经可以看到刚刚我们新建的库了。

mysql> create database mysql_learn;
Query OK, 1 row affected (0.11 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mysql_learn        |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 

之后我们需要进到库里去新建表格,在表格里面进行“增”,“删”,“改”,“查”的操作

先进入数据库使用mysql_learn; 这里我们可以看看库里都有有什么,使用show tables;(其实什么都没有)

mysql> use mysql_shiyan;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> 

然后我们使用create table命令新建表单,然后再用show tables; 确定表格已建立

mysql> create table book (book_id int(10),book_name char(40),read_end_time date);
Query OK, 0 rows affected (0.01 sec)

mysql> create table reader (reader_name char(20),book_name char(40),read_start_time date);
Query OK, 0 rows affected (0.12 sec)

mysql> show tables;
+------------------------+
| Tables_in_mysql_learn  |
+------------------------+
| book                   |
| department             |
| employee               |
| reader                 |
+------------------------+
4 rows in set (0.00 sec)

mysql> 

“增”

在表格里使用insert into (表名) values (与表头一一对应的实际数据) 命令来写入数据;

“查”

在表格里使用select * from (表名); 查看表格内的详细信息

mysql> select * from book;
+---------+-----------+---------------+
| book_id | book_name | read_end_time |
+---------+-----------+---------------+
|       1 | longzu1   | 2018-12-01    |
|       2 | longzu2   | 2018-12-07    |
|       2 | longzu3   | 2018-12-11    |
+---------+-----------+---------------+
3 rows in set (0.00 sec)

mysql> 

“改”

使用命令update (表名) set (列名)='(实际改后的内容)' where (表格第一列id);

mysql> update book set book_name='Java' where book_id=2;
Query OK, 2 rows affected (0.12 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from book;
+---------+-----------+---------------+
| book_id | book_name | read_end_time |
+---------+-----------+---------------+
|       1 | longzu1   | 2018-12-01    |
|       2 | Java      | 2018-12-07    |
|       2 | Java      | 2018-12-11    |
+---------+-----------+---------------+
3 rows in set (0.00 sec)

mysql> 

“删”

1、使用命令 delete from () where (自增字段id);  这个自增字段将起始值恢复成1,且返回删除了几条数据

mysql> select * from book;
+---------+-----------+---------------+
| book_id | book_name | read_end_time |
+---------+-----------+---------------+
|       1 | longzu1   | 2018-12-01    |
|       2 | Java      | 2018-12-07    |
|       2 | Java      | 2018-12-11    |
+---------+-----------+---------------+
3 rows in set (0.00 sec)


mysql> delete from  book where 1;
Query OK, 3 rows affected (0.05 sec)

mysql> select * from book;
Empty set (0.00 sec)

mysql>

2、delete from (表名) where (列名)=(需要删除的具体内容);

mysql> select * from reader;
+-------------+-----------+-----------------+
| reader_name | book_name | read_start_time |
+-------------+-----------+-----------------+
| chujun      | longzu1   | 2018-11-27      |
| chujun      | longzu2   | 2018-12-02      |
| Richel      | Loading   | 2018-01-01      |
+-------------+-----------+-----------------+
3 rows in set (0.01 sec)

mysql> delete from reader where reader_name='chujun';
Query OK, 2 rows affected (0.01 sec)

mysql> select * from reader;
+-------------+-----------+-----------------+
| reader_name | book_name | read_start_time |
+-------------+-----------+-----------------+
| Richel      | Loading   | 2018-01-01      |
+-------------+-----------+-----------------+
1 row in set (0.01 sec)

mysql>

表格内的增删改查基本简单用法就完了,数据库的增也记录了,明儿来补删改查吧!睡了睡了~~

你可能感兴趣的:(MySQL)