yzdily.github.io
yzdily.github.io copied to clipboard
第三章 数据基础之 MySQL数据库
首先是数据库的创建,其次是在MySQL命令行对数据增、删、改、查的练习,最后是用cmd打开phpstudy中的MySQL,以及增删改查练习。要注意的是,phpstudy中默认账户密码都是root,只在文件中把密码改了是没有用的。 在此之前,我们需要了解一下什么是数据库和mysql。 数据库就是存储数据的仓库,其本质是一个文件系统,数据按照特定的格式将数据存储起来,用户可以对数据库中的数据进行增加,修改,删除及查询操作。 mysql是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,提高了灵活性。使用的语言是SQL语言。
1、创建数据库 (1)打开 phpstudy 至少mysql是绿灯可用的 再打开 MySQL-Front 发现这里自带前端可视化界面。然后我开始对数据库进行创建。
(2)创建、删除数据库 首先,创建一个数据库名为mydata的数据库(或者是在MySQL命令行中输入create database 数据库名称;)
mysql> create database mydata;
Query OK, 1 row affected (0.01 sec)
如果删除该数据库,在这里可进行删除(或者是在MySQL命令行中输入drop database 数据库名称;)
mysql> drop database mydata;
Query OK, 0 rows affected (0.00 sec)
(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)
如果删除该数据表,在这里可进行删除(或者是在MySQL命令行中输入drop table 表名;)
mysql> drop table users;
Query OK, 0 rows affected (0.00 sec)
然后类似的,再进行字段的创建。创建好之后,由对象浏览器转换到数据浏览器,输入相关数据就完整创建了一个数据库。如下:
2、通过MySQL命令行对数据增、删、改、查 首先,打开MySQL命令行,输入默认密码root
(1)选择数据库 选择使用哪个数据库,这里选择使用的是mydata数据库
mysql> use mydata;
Database changed
(2)数据的增、删、改、查 1.增 原先,没有“id=6,text=‘qwzf’,password=‘qwzf’”这条记录,通过sql语句在数据库的表格中添加了这条记录。(或者是alter table 表名 add 字段名称+字段类型;)
2.删
mysql> delete from users where id=5;
Query OK, 1 row affected (0.00 sec)
这里删除了在表格users中id=5的那条记录。(或者是alter table 表名 drop 字段名字;)
3.改 如果要改表名,则在MySQL命令行输入alter table 原表名 rename 新的名字; 如果要修改字段,则在MySQL命令行输入alter table 表名 change 原来的字段名称 修改后的字段名称;(或者是update 表名 set 字段=‘数据’ where 范围;)在id=4的那条记录中,字段text下的数据“php”被更新修改成“him”
4.查 1.查库 在命令行想看所有的数据库,这就用到了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)
2.查表 在命令行想看mydata数据库中所有表,这就用到了show tables;
mysql> show tables;
+------------------+
| Tables_in_mydata |
+------------------+
| users |
+------------------+
1 row in set (0.00 sec)
在命令行想看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)
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)
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)
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>
用cmd打开mysql后,增、删、改、查过程与直接在phpstudy通过MySQL命令行对数据增、删、改、查,过程相同。可参照上面的直接通过MySQL命令行对数据增、删、改、查过程。