MySQL快查-对表中数据的操作

MySQL快查

因为在日常工作学习中经常忘记mysql的一些语句、关键字、操作等内容,所以最近抽取时间写了以下关于mysql相关内容。相当于一本字典吧


重置mysql密码
数据类型
运算符
常用函数
数据完整性
数据库的基本操作
对表本身的操作
本文
子查询
多表连接
索引
视图
预处理SQL语句
自定义函数与存储过程
在MySQL中编程


插入表记录

{
   
   INSERT | REPLACE} [INTO] 表名
	[(字段名 [, 字段名, ...])]
    {
   
   VALUES | VALUE}
    ([,, ...])[,(,[,, ...]), ...]

例如向表t1插入值
表t1结构:
mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int      | NO   | PRI | NULL    |       |
| name  | char(10) | YES  | UNI | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.02 sec)

# 插入一行数据
mysql> insert into t1 value(1,'老李');
Query OK, 1 row affected (0.02 sec)

mysql> insert into t1(id) values(2);
Query OK, 1 row affected (0.01 sec)

# 插入多行
mysql> insert t1 values(3, 'ab'),(4,'cd');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

insert 和 replace区别

如果表中某个或某些字段是必须唯一的(不可重复的),这时如果使用insert插入一条表中对应字段已存在的数据就会报错,但replace会先删除原先的记录,然后插入新的记录。

mysql> desc t1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int      | NO   | PRI | NULL    |       |
| name  | char(10) | YES  | UNI | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.02 sec)
# 表t1中的id是主键,主键必须唯一

# 表中的已有的记录
mysql> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  2 | NULL   |
|  3 | ab     |
|  4 | cd     |
|  1 | 老李   |
+----+--------+
4 rows in set (0.00 sec)

# 这时候用insert插入会报错
mysql> insert into t1 value(2, '老王');
ERROR 1062 (23000): Duplicate entry '2' for key 't1.PRIMARY'

# 使用replace就能成功插入
mysql> replace into t1 value(2, '老王');
Query OK, 2 rows affected (0.00 sec)
# 改变后
mysql> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  3 | ab     |
|  4 | cd     |
|  1 | 老李   |
|  2 | 老王   |  # null变成了老王
+----+--------+
4 rows in set (0.00 sec)

更多

删除表记录

DELETE FROM 表名 
    [WHERE where_condition]


# 原来表中内容
mysql> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  3 | ab     |
|  4 | cd     |
|  1 | 老李   |
|  2 | 老王   |
+----+--------+
4 rows in set (0.00 sec)

# 删除t1表中id为4的记录
mysql> delete from t1 where id = 4;
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  3 | ab     |
|  1 | 老李   |
|  2 | 老王   |
+----+--------+
3 rows in set (0.00 sec)


# 如果不写where条件将会删除表中所有记录(但不会把表删除)
mysql> delete from t1;
Query OK, 3 rows affected (0.01 sec)

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

更多

修改表记录

UPDATE  表名
    SET 字段名 =[, 字段名 =, ...]
    [WHERE where_condition]


# 原来的记录
mysql> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  2 | 阿七   |
|  1 | 老ok   |
+----+--------+
2 rows in set (0.00 sec)

# 将t1表中id为2的记录的name修改为‘老八’
mysql> update t1 set name = '老八' where id = 2;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  1 | 老ok   |
|  2 | 老八   |
+----+--------+
2 rows in set (0.00 sec)

# 不加where条件的话将会修改表中所有记录
mysql> update t1 set name = '九哥';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0
# 注:被修改的字段不能是unique约束的,并且不能是有索引的,否则会报错.

更多

查询表记录

SELECT [DISTINCT]
    select_expr [, select_expr,...] 
    [FROM 表名[, 表名,...]
    [WHERE 选择条件]
    [GROUP BY {字段名 | 表达式} [WITH ROLLUP]]
    [HAVING 选择条件]
    [ORDER BY {字段名 | 表达式} [ASC | DESC]]
    [LIMIT {
   
   [偏移量(起始行),] 行数 | 行数 OFFSET 偏移量}]]
select_expr:
	字段名 [[as] 显示的字段名]
	*
	select语句 [as]select语句取的名(当做字段名)
  • distinct 去重
  • where 筛选(过滤)
  • group by 分组
  • having 筛选,通常跟group by一起使用。将在where筛选后进一步筛选
  • order by 排序 默认升序(asc),可以指定asc(升序)desc(降序)
  • limit 限制行数
    更多
    distinct
    where
    group by
    order by
# 查询表t1中所有字段
mysql> select * from t1;
+----+--------+
| id | name   |
+----+--------+
|  1 | 九哥   |
|  2 | 九哥   |
+----+--------+
2 rows in set (0.00 sec)


# 查询表t1的记录,并限制只查一行
mysql> select * from t1 limit 1;
+----+--------+
| id | name   |
+----+--------+
|  1 | 九哥   |
+----+--------+
1 row in set (0.00 sec)

# 查询id为1的记录,并且只要name字段
mysql> select name from t1 where id = 1;
+--------+
| name   |
+--------+
| 九哥   |
+--------+
1 row in set (0.00 sec)

# 查询id大于0的记录,并降序显示
mysql> select * from t1 where id > 0 order by id desc;
+----+--------+
| id | name   |
+----+--------+
|  2 | 九哥   |
|  1 | 九哥   |
+----+--------+
2 rows in set (0.00 sec)

更加复杂的查询将在后面的文章介绍,比如连表操作、子查询等。

猜你喜欢

转载自blog.csdn.net/weixin_45345384/article/details/115680357