设为首页 收藏本站
查看: 1314|回复: 0

[经验分享] mysql常用语句

[复制链接]

尚未签到

发表于 2018-10-11 10:31:11 | 显示全部楼层 |阅读模式
  DDL (数据定义问题)
  数据定义语言 - Data Definition Language
  用来定义数据库的对象,如数据表、视图、索引等
  DML  (数据操纵问题)
  数据处理语言 - Data Manipulation Language
  在数据库表中更新,增加和删除记录
  如 update, insert, delete
  DCL (数据控制问题)
  数据控制语言 – Data Control Language
  指用于设置用户权限和控制事务语句
  如grant,revoke,if…else,while,begin transaction
  DQL (数据查询问题)
  数据查询语言 – Data Query Language
  select
  =====================================================
  登陆:
  mysql (-h 连接的主机ip -P端口3306)-u 用户名 -p 密码---( 连接本机: 省略 -h 和 -P  主机和端口。)
  查询当前的所有库
  show databases;
  查询当前数据的创建方式:查看数据的编码表
  show create database 库名;
  创建库
  create database 数据库名   由于创建数据库时没有指定编码表,因此会使用安装数据库时默认的编码表
  create database 数据库名 character set 编码表名; 创建数据库会使用指定的编码表
  create database 数据库名 character set 编码表名 collate 排序规则; 使用指定的编码表同时还可以根据编码表指定排序规则
  删除数据库
  drop database 数据库名
  修改数据库编码集
  alter database 数据库名称 character set collate 比较规则
  切换数据库和查看正在使用的数据
  use 数据库名
  show database();
  ============================================
  创建数据表
  create table 表名(列名 类型(长度) 约束)
  create table employee2(
  id int primary key auto_increment,  当前这一列是主键
  name varchar(32)  not null ,  //不能为null
  sex varchar(10),
  password varchar(32) unique not null,
  birthday date
  );
  查看创建的表的结构
  desc 表名
  数据表结构的修改
  alter table 表名 增/删/改  列名 类型(长度)约束
  1、增加列:
  alter table 表名 add 列名 类型(长度) 约束
  2、修改现有的列:
  alter table 表名 modify 列名 类型(长度)约束
  3、修改现有列的名称
  alter table 表名 change 旧列名 新列名 类型(长度)约束
  4、删除现有的列
  alter table 表名 drop 列名
  5、修改表名
  rename table 旧列名 to 新列名
  6、修改表的字符集
  alter table 表名 character set utf8;
  数据表的删除
  drop table 表名
  查看当前库中有多少表
  show tables;
  查看表的结构
  desc table 表名
  查看表的编码
  show create table 表名
  ==================================================
  数据表的增删改查
  向数据表中插入数据
  insert into 表名(列名1,...)values (值1,。。);(当值全都要写时,列名可以省略)
  修改数据记录
  update 表名 set 列名=值,。。。。where 条件语句
  注意:一般在修改数据表中的数据时,都需要加条件,而不能直接修改表中的某一列的所有值。
  update person set username=’王五’,password='123' where id =3;
  删除数据表记录
  delete from 表名 where 条件语句
  删除所有
  delete from person;
  查询数据表中的所有记录
  select * from 表名
  查询指定列记录
  select 列名,列名... form 表名 where条件语句
  ========运算符=====================================
  相等=        不等
  2) 区间:between  ...and... 在两者之间取值 between 70 and 80
  等价于 >=70 =24 and sage 磁盘SQL文件路径
  mysqldump -u root -p mydb3 > c:\mydb3.sql
  由于mysqldump命令不是sql命令,需要在dos窗口下使用。
  恢复数据,需要手动的先创建数据库:
  create database mybd3;
  mysql -u root -p mybd3 any(1,2,3)  等价于 min(1,2,3)
  all 所有数据 >all(1,2,3)   等价于 max(1,2,3)
  1、查询最高分:
  studentcource  找到最高分
  select max(score) fromstudentcource  ;
  2、需要从studentcource  表中查出 最高分对应的学生id
  select student_id from studentcource  where score = ( select max(score) fromstudentcource );
  select student_id from studentcource where score  >  all(  select score from studentcource  );
  3、根据学生的id到student表中找出学生信息
  select * from student where id in( select student_id from studentcource  where score = ( select max(score) from studentcource ) );
  select * from student where id in( select student_id from studentcource where score  >= all(  select score from studentcource  ));
  查询编号2课程比编号1课程成绩高所有学生信息:
  1、编号2  和编号1 的课程成绩:
  select score from studentcource where cource_id = 1 or cource_id = 2;
  2、查找出编号2 比 编号1 成绩高的学生id
  找出编号1的成绩的最高分
  select score from student where cource_id = 1;
  使用子查询的结果,我们可以把它当做一个临时表存在,然后在从其中找出需要的结果:
  select max(temp.score)  from   (select score from studentcource where cource_id = 1) as temp;
  找出编号2 的成绩高于 编号 1 的  学生id
  select student_id from studentcource where cource_id = 2 and score > ( select max(score) from studentcource where cource_id = 1 );
  select student_id from studentcource where cource_id = 2 and score > all( select score from studentcource where cource_id = 1 );
  select * from student where id in ( select student_id from studentcource where cource_id = 2 and score > all( select score from studentcource where cource_id = 1 ) );
  额外附加:
  思考,需要查询编号2课程比编号1课程成绩高所有学生信息 ,同时列出学生的成绩。
  如果数据来自多张表,这时需要把不同的表进行连接查询。
  思路:需要把student 表 和 studentcource 表中的 成绩现实。
  select * from student ,( select score ,student_id from studentcource where cource_id = 2 and score > all( select score from studentcource where cource_id = 1 ) )as temp  where student.id = temp.student_id ;
  2.5、mysql的自带函数
  msyql数据库中自带了一些函数。在我们使用sql语句操作mysql数据库的时候,可以直接使用这些函数。
  ===========一对多的表设计================================================================
  =================多对多之表的设计原则=====================================
  1、在任意一方添加外键都失败,要设计中间表。
  2、创建顺序,先创建“父表”。
  3、创建中间表
  4、外键约束
  5、采用联合主键(一组值不能出现两次)。
  =======================================================
  一般是在先创建完父表之后再建立表之间的关系。如下:
  create table if not exists orders(
  id int primary key auto_increment,
  name varchar(20),
  info varchar(100)
  );
  create table if not exists products(
  id int primary key auto_increment,
  name varchar(20),
  price double(10.2)
  );
  create table if not exists orders_products(
  oid int, --订单主键
  pid int,---商品主键
  buynum int --一张订单商品购买数量
  primary key(oid,pid)--联合主键
  );
  --添加主键
  ====================================================================================
  alter table orders_products add constraint orders_products_orders_fk foreign key(oid) reference orders(id);
  alter table orders_products add constraint orders_products_products_fk foreign key(pid) reference products(id);
  /*
  还有一种就是在见表的的时候就建立关系了:
  如在多方(直接在最后书写):
  constraint user_orders_fk foreign key(uid) reference user(id);
  */
  ===================================================
  在一对多的情况下,删除表的时候,先删除多的一方,再,删除一方,否则删除了表。
  =========================================================================
  =======================================================


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-620280-1-1.html 上篇帖子: mysql常用语句 下篇帖子: Mysql 特别注意点!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表