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

sql语句练习

MySQL的常见sql语句

  • 1.首先选择数据库,并删除多余的表:
use lik;
drop table account, log, tb_brand;
  • 2.创建表:学生表、课程表、以及每个学生选的课程表
create table student(id   int auto_increment comment '主键ID' primary key,name varchar(10) null comment '姓名',no   varchar(10) null comment '学号'
)comment '学生表';create table course(id int auto_increment comment '主键ID' primary key,name varchar(10) null comment '课程名称'
)comment '课程表';create table student_course(id int auto_increment comment '主键' primary key,studentid int not null comment '学生ID',courseid  int not null comment '课程ID',constraint fk_courseid foreign key (courseid) references course (id),constraint fk_studentid foreign key (studentid) references student (id)
)comment '学生课程中间表';

初级增删改查

  • 1.增:给对应的表插入数据
#插入学生信息
INSERT INTO student (name, no) VALUES ('黛绮丝', '2000100101');
INSERT INTO student (name, no) VALUES ('谢逊', '2000100102');
INSERT INTO student (name, no) VALUES ('殷天正', '2000100103');
INSERT INTO student (name, no) VALUES ('韦一笑', '2000100104');#插入课程
INSERT INTO course (name) VALUES ('Java');
INSERT INTO course (name) VALUES ('PHP');
INSERT INTO course (name) VALUES ('MySQL');
INSERT INTO course (name) VALUES ('Hadoop');#插入学生所选课程
INSERT INTO student_course (studentid, courseid) VALUES (1, 1);
INSERT INTO student_course (studentid, courseid) VALUES (1, 2);
INSERT INTO student_course (studentid, courseid) VALUES (1, 3);
INSERT INTO student_course (studentid, courseid) VALUES (2, 2);
INSERT INTO student_course (studentid, courseid) VALUES (2, 3);
INSERT INTO student_course (studentid, courseid) VALUES (3, 4);
  • 2.删:删除某个学生,以及该学生对应选的课程信息
begin; #start transaction;
delete from student  where id = 4;
delete from student_course where studentid = 4;
commit;
  • 3.改:
update student set name = '张无忌' where id = 1;
  • 4.查:多表联查
select brand_name, description from tb_brand where id = 1;select s.name name, c.name course from student s, course c, student_course sc where s.id = sc.studentid and c.id = sc.courseid;

进阶

大数据量情况下:

  • 1.批量插入:
insert into student values (null, '赵敏', 13411555), (null, '小昭', 2344144),(null, '张三丰', 13412414);
  • 2.分段式批量插入:一次插入5000~10000适当,可以分段批量插入
start transcation;
insert into student values(null, '赵敏', 13411555), (null, '小昭', 2344144),(null, '张三丰', 13412414);
insert into student values(null, '赵敏', 13411555), (null, '小昭', 2344144),(null, '张三丰', 13412414);
insert into student values(null, '赵敏', 13411555), (null, '小昭', 2344144),(null, '张三丰', 13412414);
commit;
  • 3.文件导入式插入:
    一次需要插入上百万条数据时,使用insert语句插入性能较低,此时可以使用MySQL提供的load指令进行插入。

    • 1.连接MySQL服务端时,加上参数: --local-infile
      mysql -- local-infile -u root -p
    • 2.设置全局参数local_infile为1,开启从本地加载文件导入数据的开关:
      set global local_infile = 1;
    • 3.执行load指定将准备好的数据文件中的数据加载到表结构中:
load data local infile '/root/sql.log' into table `student` fields terminated by ',' lines terminated by '\n';
  • 其中文件的数据要与表字段顺序一致,并且中间用逗号或者空格隔开,像上面的sql语句就是表示将数据以'\n'作为换行,','作为分隔符进行读取:
    在这里插入图片描述

文章转载自:

http://CSrVEDRy.dgknL.cn
http://6Wquv39h.dgknL.cn
http://P5NItYA7.dgknL.cn
http://3dX09OnJ.dgknL.cn
http://uIpyxo3i.dgknL.cn
http://orhwCRnB.dgknL.cn
http://PQ8WWn6P.dgknL.cn
http://I2V3Swqd.dgknL.cn
http://X2soQqVB.dgknL.cn
http://zwa5pbNc.dgknL.cn
http://EI287kwb.dgknL.cn
http://d23C3zp5.dgknL.cn
http://27IneMpP.dgknL.cn
http://yuOOTYVP.dgknL.cn
http://ajHmUCmR.dgknL.cn
http://X76DXoPd.dgknL.cn
http://Vc5tGPwv.dgknL.cn
http://V2TiEkhv.dgknL.cn
http://4Cj8X1UI.dgknL.cn
http://EALApL4v.dgknL.cn
http://hytT00wd.dgknL.cn
http://fyX23Yit.dgknL.cn
http://R12y0xVN.dgknL.cn
http://Bp8a8BJJ.dgknL.cn
http://RhlxNU3r.dgknL.cn
http://sNLw2c1Q.dgknL.cn
http://quMRaSlr.dgknL.cn
http://FFTjXOzz.dgknL.cn
http://tAfL0npZ.dgknL.cn
http://gWugA0Qc.dgknL.cn
http://www.dtcms.com/a/377684.html

相关文章:

  • 《深度学习的核心公式》
  • 划痕缺陷检测:3类成因,对应3套打光方案
  • 摆动序列:如何让数组“上下起伏”地最长?
  • 模型 公地悲剧
  • 深入理解 Android targetSdkVersion:从 Google Play 政策到依赖冲突
  • DDNS动态域名解析服务:原理与工具详解全攻略,外网畅访内网设备用自定义固定域名
  • 【系统分析师】第20章-关键技术:信息物理系统分析与设计(核心总结)
  • 批量更新数据:Mybatis update foreach 和 update case when 写法及比较
  • 九、瑞萨发布RZT/N认证的PROFINET-IRT和PROFIdrive软件协议栈
  • Linux系统学习之注意事项及命令基本格式
  • MoE架构训练系统设计:专家并行与门控网络优化策略
  • 深入分析神马 M56S+ 202T 矿机参数与性能特点
  • 工业RFID现场网关模块:实现多协议互通,128台读写设备互连!
  • Linux系统之----信号
  • 硅基计划4.0 算法 归并排序
  • 关于发布未来工业互联网基础理论与关键技术重大研究计划2025年度项目指南的通告
  • RAG技术解析:AI如何“边查边答”,还要守住数据安全底线?
  • 多通道相参信号
  • 数据映射表
  • NVSpeech_170k 数据集音频提取处理
  • GC Root的一些理解
  • Windows 使用 SHFileOperation 实现文件复制功能
  • Linux防火墙-Firewalld
  • 面壁智能开源多模态大模型——MiniCPM-V 4.5本地部署教程:8B参数开启多模态“高刷”时代!
  • vue3+TS项目配置Eslint+prettier+husky语法校验
  • Redis 5单线程 vs 6多线程性能解析
  • CSS 特指度 (Specificity)
  • 数据结构(C语言篇):(十一)二叉树概念介绍
  • 【go语言 | 第1篇】Go环境安装+go语言特性
  • 嵌入式面试题(4)