提示:本文章Linux中Mysql的增删改查,从创建库建表开始,适合小白学习,欢迎大佬!!!
提示:linux数据库的常用命令以及作用
数据库的常用命令以及作用 | |
---|---|
用法 | 作用 |
CREATE database 数据库名称 | 创建新的数据库 |
DESCRIBE 表单名称 | 描述表单 |
UPDATE 表单名称 SET attribute=新值 WHERE attribute > 原始值; | 更新表单中的数据 |
USE 数据库名称; | 指定使用的数据库 |
SHOW databases; | 显示当前已有的数据库 |
SHOW tables; | 显示当前数据库中的表单 |
SELECT * FROM 表单名称; | 从表单中选中某个记录值 |
DELETE FROM 表单名 WHERE attribute=值; | 从表单中删除某个记录值 |
提示:以下是本篇文章正文内容,下面案例可供参考
示例:1. 创建一个test数据库并且设置默认字符集为utf8mb4
mysql> CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
2,创建一个数据表
mysql> use test;
Database changed
mysql> create table mybook(name char(15), price int, pages int);
Query OK, 0 rows affected (0.00 sec)
mysql> desc mybook;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pages | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.04 sec)
3,再创建一个数据表myclass,设置id字段为主键,自增,并且不能为空,sex字段默认值为0。
mysql> create table myclass(id int(4) not null primary key auto_increment,name char(20) not null,sex int(4) not null default '0');
Query OK, 0 rows affected (0.22 sec)
mysql> desc myclass;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(4) | NO | PRI | NULL | auto_increment |
| name | char(20) | NO | | NULL | |
| sex | int(4) | NO | | 0 | |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
1,向mybook数据表中插一条图书信息,其中书名为Linux,价格和页数分别是60元和100页。
代码如下(示例):
mysql> insert into mybook(name,price,pages) values('Linux','60','100');
Query OK, 1 row affected (0.00 sec)
mysql> select * from mybook;
+-------+-------+-------+
| name | price | pages |
+-------+-------+-------+
| Linux | 60 | 100 |
+-------+-------+-------+
1 row in set (0.00 sec)
代码如下(示例):
mysql> update mybook set price=55 where name='Linux';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select name,price from mybook;
+-------+-------+
| name | price |
+-------+-------+
| Linux | 55 |
+-------+-------+
1 row in set (0.00 sec)
使用update命令将刚才插入的Linux图书信息的价格修改为55元
代码如下(示例):使用delete命令删除数据表mybook中的所有内容,然后再查看该表单中的内容,可以发现该表单内容为空了
mysql> delete from mybook;
Query OK, 1 row affected (0.00 sec)
mysql> select * from mybook;
Empty set (0.00 sec)
where命令中使用的参数以及作用 | |
---|---|
参数 | 作用 |
= | 相等 |
<>或!= | 不相等 |
> | 大于 |
< | 小于 |
>= | 大于或等于 |
<= | 小于或等于 |
BETWEEN | 在某个范围内 |
LIKE | 搜索一个例子 |
IN | 在列中搜索多个值 |
代码如下(示例):分别在mybook数据表中查找出价格大于35元或价格不等于50元的图书
mysql> select * from mybook where price>35;
+--------+-------+-------+
| name | price | pages |
+--------+-------+-------+
| Linux2 | 40 | 200 |
| Linux3 | 50 | 300 |
| Linux4 | 60 | 400 |
+--------+-------+-------+
3 rows in set (0.00 sec)
mysql> select * from mybook where price!=50;
+--------+-------+-------+
| name | price | pages |
+--------+-------+-------+
| Linux1 | 30 | 100 |
| Linux2 | 40 | 200 |
| Linux4 | 60 | 400 |
+--------+-------+-------+
3 rows in set (0.00 sec)
代码如下(示例):批量删除多条记录
mysql> select * from mybook;
+--------+-------+-------+
| name | price | pages |
+--------+-------+-------+
| Linux1 | 30 | 100 |
| Linux2 | 40 | 200 |
| Linux3 | 50 | 300 |
| Linux4 | 60 | 400 |
+--------+-------+-------+
4 rows in set (0.00 sec)
mysql> delete from mybook where name in ('Linux1','Linux3');
Query OK, 2 rows affected (0.00 sec)
mysql> select * from mybook;
+--------+-------+-------+
| name | price | pages |
+--------+-------+-------+
| Linux2 | 40 | 200 |
| Linux4 | 60 | 400 |
+--------+-------+-------+
2 rows in set (0.00 sec)
提示:原文地址