MySQL数据库 SQL语言2

SQL语言

DML

数据操纵语言(Data Manipulation Language, DML)是用于数据库操作,对数据库其中的对象和数据运行访问工作的编程语句,通常是数据库专用编程语言之中的一个子集,简单来说就是增删改查。
INSERT 实现数据的 插入
DELETE 实现数据的 删除
UPDATE 实现数据的 更新。

  1. 插入数据insert
    完整插入:
    语法:insert into 表名 values(字段1 类型1,字段2 类型2);
    部分插入:
    语法:insert into 表名 (列名,列名) values(字段1 类型1,字段1 类型1)
  2. 更新数据update
    语法:update 表名 set 列名=数值 where 条件
    修改MySQL数据库管理员root账户的密码
mysql> Update mysql.user set authentication_string=password(‘ZhongGuo@123) where user=’root’
  1. 删除数据库delete
    语法:delete from 表名 where id=2

DQL

在MySQL管理软件中,可以通过SQL语句中的DQL语言来实现数据的
Select查询操作,互联网用户查询余额,查询装备,查询商品的操作。

Mysql查询

  1. 准备环境
  • 创建库:
mysql> create database company
  • 创建表:
mysql>  CREATE TABLE company.employee5(
     id int primary key AUTO_INCREMENT not null,
    name varchar(30) not null,
    sex enum('male','female') default 'male' not null,
     hire_date date not null,
     post varchar(50) not null,
     job_description varchar(100),
     salary double(15,2) not null,
     office int,
     dep_id int
     );
  • 查看表结构:
mysql> desc employee5;
  • 插入数据:
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values 
('jack','male','20180202','instructor','teach',5000,501,100),
('tom','male','20180203','instructor','teach',5500,501,100),
('robin','male','20180202','instructor','teach',8000,501,100),
('alice','female','20180202','instructor','teach',7200,501,100),
('aofa','male','20180202','hr','hrcc',600,502,101),
('harry','male','20180202','hr',NULL,6000,502,101),
('emma','female','20180206','sale','salecc',20000,503,102),
('christine','female','20180205','sale','salecc',2200,503,102),
('zhuzhu','male','20180205','sale',NULL,2200,503,102),
('gougou','male','20180205','sale','',2200,503,102);

在这里插入图片描述
2. 简单查询
查看所有列:select * from 表名;
查看部分列:select 列1,列2,列3, from 表名;
注意:前提是需要进入数据库,不进入数据库,就输入库名。
例:看年薪:

mysql> select name,salary,salary*14 from employee5;
  • 单条件查询where:
    查询hr部门的员工名字
mysql> Select  name,post  from  employee5  where post=’hr’;
  • 多条件查询and/or
    查询hr部门员工名字,工资大于1000
mysql> Select  name,salary  from employee5  where post=’hr’ and salary>1000;

查询部门所有员工,工资是6000或8000的员工

mysql> Select  name,salary from employee5  where salary=6000 or salary=8000;
  • 关键字between and 在什么之间
    查询工资在5000~15000的员工
mysql> Select  name,salary  from employee5 where salary between 5000 and 15000;

工资不在5000~15000范围 使用not between 5000 and 15000;

  • 关键字in集合查询
    工资4000,或5000,或9000的员工
mysql> Select name,salary from employee5 where salary in(4000,5000,9000);
  • 关键字is null
    没有岗位描述的员工
mysql> Select name,job_description from emloyee5 where job_description is null;
  • 关键字like 模糊查询
    查询叫a的员工
mysql> Select name from employee5 where name like ‘a%;
  1. 查询排序
    工资升序排列
    Select * from 表名 order by 工资列名 asc;
    工资降序排列
    Select * from 表名 order by 工资列名 desc;
    工资最高的前五名
    Select * from 表名 order by 工资列名 desc limit 5;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值