Mysql增删改查

点击查看代码
mysql> CREATE DATABASE school;
Query OK, 1 row affected (0.00 sec)

mysql> USE school;
Database changed
mysql> CREATE TABLE stu
    -> (
    -> id INT(10),
    -> name VARCHAR(25),
    -> score INT(20)
    -> );
Query OK, 0 rows affected (0.01 sec)

INSERT INTO stu VALUES(3,'xiaox',93);
DELETE FROM stu WHERE id=1;


mysql> select * from stu;
+------+-----------+-------+
| id   | name      | score |
+------+-----------+-------+
|    2 | xiaozhang |    95 |
|    1 | xiaoli    |    90 |
|    3 | xiaox     |    93 |
+------+-----------+-------+
3 rows in set (0.00 sec)

mysql> select name from stu;//查
+-----------+
| name      |
+-----------+
| xiaozhang |
| xiaoli    |
| xiaox     |
+-----------+
3 rows in set (0.00 sec)

mysql> select * from stu;
+------+-----------+-------+
| id   | name      | score |
+------+-----------+-------+
|    2 | xiaozhang |    95 |
|    1 | xiaoli    |    90 |
|    3 | xiaox     |    93 |
+------+-----------+-------+
3 rows in set (0.00 sec)

mysql> update stu set score=100 where score=93;//改
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from stu;
+------+-----------+-------+
| id   | name      | score |
+------+-----------+-------+
|    2 | xiaozhang |    95 |
|    1 | xiaoli    |    90 |
|    3 | xiaox     |   100 |
+------+-----------+-------+
3 rows in set (0.00 sec)

------------

<details>
<summary>点击查看代码</summary>

sudo mysql //qidong
sudo systemctl status mysql
service mysql start
service mysql stop

一、SQL语句之增

INSERT INTO 表名 VALUES (值1,....)

向student表中插入一个学生的数据

insert into student (num,name,sex,age)

values(140010,张三,男,23)

二、SQL语句之删

DELETE FROM 表名称 WHERE 列名称 = 值

删除student表中num=140011的这条数据。

delete from student where num=140011;

三、SQL语句之改

UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值

我们可以将num为140010的age值更改为21。

update student set age =21 where ID=140010;

四、SQL语句之查

SELECT 列名称 FROM 表名称

//以及:

SELECT * FROM 表名称

查询语句非常的重要的,所以需要详细来说一下。

1、查询student表中所有数据

select * from student;

2、查询student表中所有的name和sex

select name,sex from student;

3、查询num为140010这一行的数据

select * from student where id =140010;

</details>


查看所有数据库

点击查看代码
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
6 row in set (0.22 sec)

posted @ 2021-11-05 13:56  上官鸿信の  阅读(21)  评论(0编辑  收藏  举报