7

phpstudy-MySQL的简单使用以及cmd打开MySQL

 2 years ago
source link: https://qwzf.github.io/2019/03/15/phpstudy-MySQL%E5%9F%BA%E7%A1%80%E7%AF%87%E4%B8%80/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

今天尝试学习使用phpstudy中的mysql,让我受益颇深。所以把学习和练习过程记录了一下,首先是数据库的创建,其次是在MySQL命令行对数据增、删、改、查的练习,最后是用cmd打开phpstudy中的MySQL,以及增删改查练习。要注意的是,phpstudy中默认账户密码都是root,只在文件中把密码改了是没有用的。
在此之前,我们需要了解一下什么是数据库和mysql。
数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户可以对数据库中的数据进行增加,修改,删除及查询操作。
mysql是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,提高了灵活性。使用的语言是SQL语言。

1、创建数据库

(1)打开MySQL-Front

在这里插入图片描述
在这里插入图片描述
发现这里自带前端可视化界面。然后我开始对数据库进行创建。

(2)创建、删除数据库

首先,创建一个数据库名为mydata的数据库(或者是在MySQL命令行中输入create database 数据库名称;

在这里插入图片描述
在这里插入图片描述
mysql> create database mydata;
Query OK, 1 row affected (0.01 sec)
sql复制代码

如果删除该数据库,在这里可进行删除(或者是在MySQL命令行中输入drop database 数据库名称;

在这里插入图片描述
在这里插入图片描述
mysql> drop database mydata;
Query OK, 0 rows affected (0.00 sec)
sql复制代码

(3)创建、删除数据表

创建一个数据表名为users的数据表格(或者是在MySQL命令行中输入create table 表名 ( id int ,name varchar(35),password varchar(40)); 其中 id name password 是字段 ,后面的限制是类型。)

在这里插入图片描述
在这里插入图片描述
mysql> use mydata;
Database changed
mysql> create table users(Id int,text varchar(35),password varchar(40));
Query OK, 0 rows affected (0.01 sec)
sql复制代码

如果删除该数据表,在这里可进行删除(或者是在MySQL命令行中输入drop table 表名;

在这里插入图片描述
在这里插入图片描述
mysql> drop table users;
Query OK, 0 rows affected (0.00 sec)
sql复制代码

然后类似的,再进行字段的创建。创建好之后,由对象浏览器转换到数据浏览器,输入相关数据就完整创建了一个数据库。如下:

在这里插入图片描述
在这里插入图片描述

2、通过MySQL命令行对数据增、删、改、查

首先,打开MySQL命令行,输入默认密码root

在这里插入图片描述
在这里插入图片描述

(1)选择数据库

选择使用哪个数据库,这里选择使用的是mydata数据库

mysql> use mydata;
Database changed
sql复制代码

(2)数据的增、删、改、查

原先,没有“id=6,text=‘qwzf’,password=‘qwzf’”这条记录,通过sql语句在数据库的表格中添加了这条记录。(或者是alter table 表名 add 字段名称+字段类型;

在这里插入图片描述
在这里插入图片描述
    mysql> delete from users where id=5;
    Query OK, 1 row affected (0.00 sec)
sql复制代码

这里删除了在表格users中id=5的那条记录。(或者是alter table 表名 drop 字段名字;

如果要改表名,则在MySQL命令行输入alter table 原表名 rename 新的名字;
如果要修改字段,则在MySQL命令行输入alter table 表名 change 原来的字段名称 修改后的字段名称;(或者是update 表名 set 字段=‘数据’ where 范围;)在id=4的那条记录中,字段text下的数据“php”被更新修改成“him”

在这里插入图片描述
在这里插入图片描述

在命令行想看所有的数据库,这就用到了show databases;(注意:在mysql语句中,一句话写完必须加分号。且mysql语句中不区分大小写)

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mydata             |
    | myl                |
    | myphp              |
    | myphp2             |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    8 rows in set (0.01 sec)
sql复制代码

在命令行想看mydata数据库中所有表,这就用到了show tables;

     mysql> show tables;
        +------------------+
        | Tables_in_mydata |
        +------------------+
        | users            |
        +------------------+
        1 row in set (0.00 sec)
sql复制代码

在命令行想看users表中所有数据,这就用到了select*from 表名;

    mysql> select*from users;
    +----+-------+----------+
    | Id | text  | password |
    +----+-------+----------+
    |  1 | admin | admin    |
    |  2 | root  | root     |
    |  3 | wang  | wang     |
    |  4 | him   | php      |
    |  6 | qwzf  | qwzf     |
    +----+-------+----------+
    5 rows in set (0.00 sec)
sql复制代码
3.查字段

在命令行想看users表中在一定范围的字段,select*from 表名 where 范围;

    mysql> select*from users where id=1;
    +----+-------+----------+
    | Id | text  | password |
    +----+-------+----------+
    |  1 | admin | admin    |
    +----+-------+----------+
    1 row in set (0.00 sec)

    mysql> select*from users where text='qwzf';
    +----+------+----------+
    | Id | text | password |
    +----+------+----------+
    |  6 | qwzf | qwzf     |
    +----+------+----------+
    1 row in set (0.00 sec)

    mysql> select*from users where password='wang';
    +----+------+----------+
    | Id | text | password |
    +----+------+----------+
    |  3 | wang | wang     |
    +----+------+----------+
    1 row in set (0.00 sec)
sql复制代码
4.查看表结构

在命令行想看users表的表结构,用desc 表名;

    mysql> desc users;
    +----------+--------------+------+-----+---------+----------------+
    | Field    | Type         | Null | Key | Default | Extra          |
    +----------+--------------+------+-----+---------+----------------+
    | Id       | int(11)      | NO   | PRI | NULL    | auto_increment |
    | text     | varchar(255) | YES  |     | NULL    |                |
    | password | varchar(255) | YES  |     | NULL    |                |
    +----------+--------------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)
sql复制代码

3、用cmd打开phpstudy中的MySQL

首先,打开cmd命令行页面。如果计算机是Windows10操作系统,输入Windows+R,打开windows运行搜索框,输入cmd回车,打开cmd命令行页面。然后在电脑上找到phpstudy所在目录。
1.返回上一级目录cd\ 2.进入下一级目录cd+空格+目录名
下面是我在cmd命令行中打开phpstudy中MySQL的过程:

    C:\Users\ASUS>cd\      // 返回上一级目录

    C:\>cd phpstudy           //进入下一级目录

    C:\phpStudy>cd PHPTutorial

    C:\phpStudy\PHPTutorial>cd MySQL

    C:\phpStudy\PHPTutorial\MySQL>cd bin

    C:\phpStudy\PHPTutorial\MySQL\bin>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.5.53 MySQL Community Server (GPL)

    Copyright (c) 2000, 2016, 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>
sql复制代码

用cmd打开mysql后,增、删、改、查过程与直接在phpstudy通过MySQL命令行对数据增、删、改、查,过程相同。可参照上面的直接通过MySQL命令行对数据增、删、改、查过程。本人小白一枚,如有个别错误,敬请大佬指正。


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 [email protected]

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK