当前位置: 首页 > wzjs >正文

临沂网站制作哪家好杭州seo 云优化科技

临沂网站制作哪家好,杭州seo 云优化科技,html网站地图生成工具,深圳开发网站建设文章目录 👉表的增删改查👈CreateRetrieve 👉表的增删改查👈 CRUD 是数据库中常用的术语,表示对数据进行增、删、改、查的操作。具体来说,CURD 语句分为以下四种: Create(创建&…

文章目录

  • 👉表的增删改查👈
    • Create
    • Retrieve

👉表的增删改查👈

CRUD 是数据库中常用的术语,表示对数据进行增、删、改、查的操作。具体来说,CURD 语句分为以下四种:

  • Create(创建):用于创建新表、新列或新的约束等数据库对象。
  • Retrieve(读取):用于查询表中的数据。
  • Update(更新):用于更新表中的数据。
  • Delete(删除):用于删除表中的数据。

Create

  • 语法
insert [into] table_name
[(column [, column] ...)]
values (value_list) [, (value_list)] ...value_list: value, [, value] ...

注:使用 insert 语句前,表必须要存在。

案例

create table if not exists students(
id int unsigned primary key auto_increment,
sn int unsigned unique key not null comment "学生的学号",
name varchar(64) not null comment "学生的姓名",
qq varchar(64) unique key );

单行数据+全列插入

#默认全列插入
mysql> insert into students values(1, 1234, '张飞', '1234@qq.com');
Query OK, 1 row affected (0.00 sec)mysql> select * from students;
+----+------+--------+-------------+
| id | sn   | name   | qq          |
+----+------+--------+-------------+
|  1 | 1234 | 张飞   | 1234@qq.com |
+----+------+--------+-------------+
1 row in set (0.00 sec)

注意:

  • 插入数据记录时,value_list 数量必须和定义表的列的数量及顺序一致。
  • 在插入数据记录的时候,也可以不用指定 id(当然,那时候就需要明确插入数据到哪些些列了),那么 mysql 会使用默认的值进行自增。

多行数据+指定列插入

mysql> insert into students (sn, name, qq) values-> (1235, '关羽', '1235@qq.com'),-> (1236, '赵云', '1236@qq.com');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0mysql> select * from students;
+----+------+--------+-------------+
| id | sn   | name   | qq          |
+----+------+--------+-------------+
|  1 | 1234 | 张飞   | 1234@qq.com |
|  3 | 1235 | 关羽   | 1235@qq.com |
|  6 | 1236 | 赵云   | 1236@qq.com |
+----+------+--------+-------------+
3 rows in set (0.00 sec)

插入否则更新
主键或者唯一键对应的值已经存在会导致插入数据记录失败。

# 主键和唯一键冲突
mysql> insert into students values(1, 1237, '刘备', '1237@qq.com');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> insert into students values(7, 1236, '刘备', '1236@qq.com');
ERROR 1062 (23000): Duplicate entry '1236' for key 'sn'

同步更新操作语法:

insert... on duplicate key update
column = value [, column = value] ..
-- on duplicate key 当发生重复key的时候,进行更新
mysql> insert into students (id, sn, name) values(6, 1238, '刘备') on duplicate key update sn=1238, name='刘备';
Query OK, 2 rows affected (0.00 sec)-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,并且数据已经被更新mysql> select * from students;
+----+------+--------+-------------+
| id | sn   | name   | qq          |
+----+------+--------+-------------+
|  1 | 1234 | 张飞   | 1234@qq.com |
|  3 | 1235 | 关羽   | 1235@qq.com |
|  6 | 1238 | 刘备   | 1236@qq.com |
+----+------+--------+-------------+
3 rows in set (0.00 sec)mysql> insert into students (id, sn, name) values(6, 1238, '刘备') on duplicate key update sn=1238, name='刘备';
Query OK, 0 rows affected (0.00 sec)#通过 MySQL 函数获取受到影响的数据行数
mysql> select ROW_COUNT();
+-------------+
| ROW_COUNT() |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)#如果更新的值其他具有唯一性的值冲突时,需要避免该冲突
mysql> insert into students (id, sn, name) values(6, 1235, '诸葛亮') on duplicate key update sn=1235, name='诸葛亮';
ERROR 1062 (23000): Duplicate entry '1235' for key 'sn'mysql> insert into students (id, sn, name) values(6, 1235, '诸葛亮') on duplicate key update sn=1236, name='诸葛亮';
Query OK, 2 rows affected (0.01 sec)mysql> select * from students;
+----+------+-----------+-------------+
| id | sn   | name      | qq          |
+----+------+-----------+-------------+
|  1 | 1234 | 张飞      | 1234@qq.com |
|  3 | 1235 | 关羽      | 1235@qq.com |
|  6 | 1236 | 诸葛亮    | 1236@qq.com |
+----+------+-----------+-------------+
3 rows in set (0.00 sec)

替换

-- 主键 或者 唯一键 没有冲突,则直接插入;
-- 主键 或者 唯一键 如果冲突,则删除后再插入
mysql> replace into students (sn, name) values(1237, '曹操');
Query OK, 1 row affected (0.00 sec)mysql> select * from students;
+----+------+-----------+-------------+
| id | sn   | name      | qq          |
+----+------+-----------+-------------+
|  1 | 1234 | 张飞      | 1234@qq.com |
|  3 | 1235 | 关羽      | 1235@qq.com |
|  6 | 1236 | 诸葛亮    | 1236@qq.com |
| 11 | 1237 | 曹操      | NULL        |
+----+------+-----------+-------------+
4 rows in set (0.01 sec)mysql> replace into students (sn, name) values(1237, '黄月英');
Query OK, 2 rows affected (0.01 sec)mysql> select * from students;
+----+------+-----------+-------------+
| id | sn   | name      | qq          |
+----+------+-----------+-------------+
|  1 | 1234 | 张飞      | 1234@qq.com |
|  3 | 1235 | 关羽      | 1235@qq.com |
|  6 | 1236 | 诸葛亮    | 1236@qq.com |
| 12 | 1237 | 黄月英    | NULL        |
+----+------+-----------+-------------+
4 rows in set (0.00 sec)-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,删除后重新插入

Retrieve

select
[distinct] {* | {column [, column] ...} #去重
[from table_name]
[where ...] #查询条件
[order by column [asc | desc], ...] #升序/降序
limit ...#显式几行数据记录

案例

-- 创建表结构
CREATE TABLE exam_result (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20) NOT NULL COMMENT '同学姓名',
chinese float DEFAULT 0.0 COMMENT '语文成绩',
math float DEFAULT 0.0 COMMENT '数学成绩',
english float DEFAULT 0.0 COMMENT '英语成绩'
);-- 插入测试数据
INSERT INTO exam_result (name, chinese, math, english) VALUES
('唐三藏', 67, 98, 56),
('孙悟空', 87, 78, 77),
('猪悟能', 88, 98, 90),
('曹孟德', 82, 84, 67),
('刘玄德', 55, 85, 45),
('孙权', 70, 73, 78),
('宋公明', 75, 65, 30);-- 查看表数据
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.01 sec)

select 列
全列查询

  • 通常情况下不建议使用 * 进行全列查询。
  • 查询的列越多,意味着需要传输的数据量越大。
  • 可能会影响到索引的使用。
# 全列查询
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  1 | 唐三藏    |      67 |   98 |      56 |
|  2 | 孙悟空    |      87 |   78 |      77 |
|  3 | 猪悟能    |      88 |   98 |      90 |
|  4 | 曹孟德    |      82 |   84 |      67 |
|  5 | 刘玄德    |      55 |   85 |      45 |
|  6 | 孙权      |      70 |   73 |      78 |
|  7 | 宋公明    |      75 |   65 |      30 |
+----+-----------+---------+------+---------+
7 rows in set (0.01 sec)

指定列查询

  • 指定列的顺序不需要按定义表的顺序来。
mysql> select id from exam_result;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
+----+
7 rows in set (0.00 sec)mysql> select id, name, chinese from exam_result;
+----+-----------+---------+
| id | name      | chinese |
+----+-----------+---------+
|  1 | 唐三藏    |      67 |
|  2 | 孙悟空    |      87 |
|  3 | 猪悟能    |      88 |
|  4 | 曹孟德    |      82 |
|  5 | 刘玄德    |      55 |
|  6 | 孙权      |      70 |
|  7 | 宋公明    |      75 |
+----+-----------+---------+
7 rows in set (0.00 sec)

查询字段为表达式

-- 表达式不包含字段
mysql> select name, id, 10 from exam_result;
+-----------+----+----+
| name      | id | 10 |
+-----------+----+----+
| 唐三藏    |  1 | 10 |
| 孙悟空    |  2 | 10 |
| 猪悟能    |  3 | 10 |
| 曹孟德    |  4 | 10 |
| 刘玄德    |  5 | 10 |
| 孙权      |  6 | 10 |
| 宋公明    |  7 | 10 |
+-----------+----+----+
7 rows in set (0.00 sec)mysql> select name, id, 10+10 from exam_result;
+-----------+----+-------+
| name      | id | 10+10 |
+-----------+----+-------+
| 唐三藏    |  1 |    20 |
| 孙悟空    |  2 |    20 |
| 猪悟能    |  3 |    20 |
| 曹孟德    |  4 |    20 |
| 刘玄德    |  5 |    20 |
| 孙权      |  6 |    20 |
| 宋公明    |  7 |    20 |
+-----------+----+-------+
7 rows in set (0.00 sec)-- 表达式包含一个字段(英语分数+10)
mysql> select id, name, english + 10 from exam_result;
+----+-----------+--------------+
| id | name      | english + 10 |
+----+-----------+--------------+
|  1 | 唐三藏    |           66 |
|  2 | 孙悟空    |           87 |
|  3 | 猪悟能    |          100 |
|  4 | 曹孟德    |           77 |
|  5 | 刘玄德    |           55 |
|  6 | 孙权      |           88 |
|  7 | 宋公明    |           40 |
+----+-----------+--------------+
7 rows in set (0.00 sec)-- 表达式包含多个字段(计算总分)
mysql> select id, name, chinese + math + english from exam_result;
+----+-----------+--------------------------+
| id | name      | chinese + math + english |
+----+-----------+--------------------------+
|  1 | 唐三藏    |                      221 |
|  2 | 孙悟空    |                      242 |
|  3 | 猪悟能    |                      276 |
|  4 | 曹孟德    |                      233 |
|  5 | 刘玄德    |                      185 |
|  6 | 孙权      |                      221 |
|  7 | 宋公明    |                      170 |
+----+-----------+--------------------------+
7 rows in set (0.00 sec)-- select也可以直接计算表达式
mysql> select 1+2+3+4+5;
+-----------+
| 1+2+3+4+5 |
+-----------+
|        15 |
+-----------+
1 row in set (0.00 sec)

为查询结果指定别名

SELECT column [AS] alias_name [...] FROM table_name;
mysql> select id, name, chinese+math+english as total from exam_result;
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  1 | 唐三藏    |   221 |
|  2 | 孙悟空    |   242 |
|  3 | 猪悟能    |   276 |
|  4 | 曹孟德    |   233 |
|  5 | 刘玄德    |   185 |
|  6 | 孙权      |   221 |
|  7 | 宋公明    |   170 |
+----+-----------+-------+
7 rows in set (0.00 sec)mysql> select id 学号, name 名字, chinese+math+english 总分 from exam_result;
+--------+-----------+--------+
| 学号   | 名字      | 总分   |
+--------+-----------+--------+
|      1 | 唐三藏    |    221 |
|      2 | 孙悟空    |    242 |
|      3 | 猪悟能    |    276 |
|      4 | 曹孟德    |    233 |
|      5 | 刘玄德    |    185 |
|      6 | 孙权      |    221 |
|      7 | 宋公明    |    170 |
+--------+-----------+--------+
7 rows in set (0.00 sec)

结果去重

  • 一行数据记录完全相同才能被看做是重复的数据记录
-- 98分重复了
mysql> select math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   98 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+
7 rows in set (0.00 sec)-- 去重结果
mysql> select distinct math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+
6 rows in set (0.00 sec

where 条件
比较运算符
在这里插入图片描述
逻辑运算符
在这里插入图片描述
数学成绩大于等于 80 分的同学及数学成绩

mysql> select name, math from exam_result where math>=80;
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 猪悟能    |   98 |
| 曹孟德    |   84 |
| 刘玄德    |   85 |
+-----------+------+
4 rows in set (0.00 sec)

数学成绩等于98分的同学

mysql> select name, math from exam_result where math=98;
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 猪悟能    |   98 |
+-----------+------+
2 rows in set (0.00 sec)
http://www.dtcms.com/wzjs/110052.html

相关文章:

  • wordpress百度主动推送湘潭关键词优化公司
  • 怎么做网站内的搜索网络营销专业学什么
  • 做网站运营经理的要求北京学电脑的培训机构
  • python在线网站台州网站seo
  • 哪些网站做兼职可靠吗网络营销的方法有哪些
  • 鹤壁做网站中视频自媒体账号注册下载
  • 网站建设一般要多钱seo外链推广工具
  • 做网站推广托管注意百度网盘搜索引擎入口在哪
  • 中软国际保定seo推广外包
  • 网站建设教程论坛网络销售平台有哪些软件
  • 图书租借网站 开发搜索广告优化
  • 做网站用什么平台搜索引擎优化英文简称
  • 宿迁网站建设案例怎样设计一个网页
  • 魔站网站建设网站推广具体内容
  • 手机网站建设万网sem技术培训
  • 在线做视频的网站查域名备案信息查询
  • 网站建设设计师的工作内容什么时候网络推广
  • 建网站收费吗抚州网络推广
  • 学校门户网站模板百度登录注册
  • java 做直播网站有哪些seo是指搜索引擎营销
  • 网站转化率低的原因百度一下app
  • 通辽住房和城乡建设委员会网站重庆百度seo代理
  • 北京微网站设计开发服务优化防控措施
  • 做电影资源网站服务器怎么选海外推广渠道都有哪些
  • 做网站无锡太原网站制作推广
  • 梦幻西如何建立网站做代练baidu百度首页官网
  • 怎样制作网站教程哪家好哪个平台做推广效果好
  • 做片子 我们是认真的网站文大侠seo
  • 上海达安做的无创dna网站手把手教你优化网站
  • 上海企业建站咨询排行榜123网