首页 > 数据库 >数据库基本操作SQL

数据库基本操作SQL

时间:2023-09-10 19:56:49浏览次数:42  
标签:salary name 数据库 SQL t2 mysql 基本操作 where select

数据库基本操作SQL

1、数据库增删改

# 创建表
mysql> create table t1(id int,
name varchar(10),
sex enum('man','gril'),
age int);

# 插入数据
#1.插⼊完整数据, 顺序插⼊: INSERT INTO 表名(字段1,字段2,字段n) VALUES (值1,值2,值 n);
mysql> insert into t1(id,name,sex,age) values ("1","wing","man","18");
Query OK, 1 row affected (0.01 sec)

#2.插⼊完整数据, 推荐⽅式 INSERT INTO 表名 VALUES (值1,值2,值n);
mysql> insert into t1 values("2","wing1","gril","10");
Query OK, 1 row affected (0.01 sec)

#3.指定字段插⼊, INSERT INTO 表名(字段2,字段3…) VALUES (值 2,值3…);
mysql> insert into t1(name,sex,age) values ("wing2","man","20");
Query OK, 1 row affected (0.00 sec)

#4.插⼊多条记录, INSERT INTO 表名 VALUES (值1,值2,值n),(值1,值2,值n);
mysql> insert into t1 values
("3","wing3","man","18"),
("4","wing4","man","18"),
("5","wing5","man","18");

# 更新数据update语句
mysql> update t1 set name="update_w1" where name="wing1";

# 删除数据
mysql> delete from t1 where name="update_w1";

2、修改数据库密码

mysql> select user,host,authentication_string from mysql.user;
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *4927A5B79C852EA2CE585A7679C5C26DF683F18C |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
//更新字段
mysql> update mysql.user set
authentication_string=password("Wing@123")
where user='root' and host='localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges;

3、数据库查询语句

//查看表字段与表信息
mysql> desc t2;
+--------+--------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| sex | enum('man','gril') | NO | | man | |
| time | date | NO | | NULL | |
| post | varchar(50) | NO | | NULL | |
| job | varchar(100) | YES | | NULL | |
| salary | double(15,2) | NO | | NULL | |
| office | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | | NULL | |
+--------+--------------------+------+-----+---------+----------------+

#2.指定字段查询
mysql> select name,salary,dep_id from t2;

#3.避免重复查询字段distinct
mysql> select distinct post from t2;

#4.设置别名。计算年薪并定义输出字段信息别名, AS可去掉
mysql> select name,salary,salary*14 AS Annual_salary from t2;
+-------+----------+---------------+
| name | salary | Annual_salary |
+-------+----------+---------------+
| jack  | 5000.00   | 70000.00 |
| tom   | 5500.00   | 77000.00 |
| robin | 8000.00   | 112000.00 |
| alice | 7200.00   | 100800.00 |
| wing  | 600.00    | 8400.00 |
| harry | 6000.00   | 84000.00 |
| trf   | 20000.00  | 280000.00 |
| test  | 2200.00   | 30800.00 |
| dog   | 2200.00   | 30800.00 |
| alex  | 2200.00   | 30800.00 |
+-------+----------+---------------+

#5.定义显示格式 CONCAT() 函数⽤于连接字符串
mysql> select concat(name,' annual salary:',salary*14) from t2;
+------------------------------------------+
| concat(name,' annual salary:',salary*14) |
+------------------------------------------+
| jack annual salary:70000.00 |
| tom annual salary:77000.00 |
| robin annual salary:112000.00 |
| alice annual salary:100800.00 |
| wing annual salary:8400.00 |
| harry annual salary:84000.00 |
| trf annual salary:280000.00 |
| test annual salary:30800.00 |
| dog annual salary:30800.00 |
| alex annual salary:30800.00 |
+------------------------------------------+


# 1.单条件查询
mysql> select name,post from t2 where post='hr';
+-------+------+
| name | post |
+-------+------+
| wing | hr |
| harry | hr |

#2.多条件查询
mysql> select name,post,salary from t2 where post='hr' and salary >5000;
+-------+------+---------+
| name | post | salary |
+-------+------+---------+
| harry | hr | 6000.00 |

#3.查找薪资范围在8000-2000,使⽤BETWEEN区间
mysql> select name,salary from t2 where salary between 8000 and 20000;
+-------+----------+
| name | salary |
+-------+----------+
| robin | 8000.00 |
| trf | 20000.00 |
+-------+----------+

#4.查找部⻔为Null, 没有部⻔的员⼯
mysql> select name,job from t2 where job is null;
+-------+------+
| name | job |
+-------+------+
| harry | NULL |
| dog | NULL |
+-------+------+

# 查看部⻔为空的员⼯
mysql> select name,job from t2 where job='';
+------+------+
| name | job |
+------+------+
| alex | |
+------+------+

#5.集合查询
mysql> select name,salary from t2 where salary=4000 OR salary=5000 OR salary=8000;
mysql> select name,salary from t2 where salary in(4000,5000,8000);
+-------+---------+
| name | salary |
+-------+---------+
| jack | 5000.00 |
| robin | 8000.00 |
+-------+---------+

#6.模糊查询like, 通配符%
mysql> select * from t2 where name like 'al%';
+----+-------+------+------------+------------+-------+---------+--------+--------+
| id | name | sex | time | post | job | salary | office | dep_id |
+----+-------+------+------------+------------+-------+---------+--------+--------+
| 4 | alice | gril | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
| 10 | alex | man | 2018-02-05 | sale | | 2200.00 | 503 | 102 |
+----+-------+------+------------+------------+-------+---------+--------+--------+

#通配符__
mysql> select * from t2 where name like 'al__';
+----+------+-----+------------+------+------+---------+--------+--------+
| id | name | sex | time | post | job | salary | office | dep_id |
+----+------+-----+------------+------+------+---------+--------+--------+
| 10 | alex | man | 2018-02-05 | sale | | 2200.00 | 503 | 102

# 查询排序
#1.按单列排序, 按薪⽔从低到⾼排序, 默认ASC(表示升序排列)
mysql> select * from t2 ORDER BY salary ASC;


# 按单列排序, 薪⽔从低往⾼排序, DESC表示倒序
mysql> select * from t2 ORDER BY salary DESC;

#2.多列排序, 先按⼊职时间,再按薪⽔排序
mysql> select * from t2 ORDER BY time DESC, salary ASC;

#3.多列排序, 先按职位, 再按薪⽔排序
mysql> select * from t2 ORDER BY post, salary DESC;

#4.查询薪资最⾼前5名同事, 默认初始位置为0
mysql> select * from t2 ORDER BY salary DESC limit 5;

#5. 从第4条开始, 并显示5条数据
mysql> select * from t2 ORDER BY salary DESC limit 3,5;
## 使⽤集合函数查询
#统计当前表总共多少条数据
mysql> select count(*) from t2;

#统计dep_id为101有多少条数据
mysql> select count(*) from t2 where dep_id=101;

#薪⽔最⾼
mysql> select MAX(salary) from t2;

#薪⽔最低
mysql> select min(salary) from t2

#平均薪⽔
mysql> select avg(salary) from t2;

#总共发放多少薪⽔
mysql> select sum(salary) from t2;

#hr部⻔发放多少薪⽔
mysql> select sum(salary) from t2 where post='hr';

#哪个部⻔哪个⼈薪⽔最⾼
mysql> select * from t2 where salary=(select max(salary) from t2);

## 分组查询
# GROUP BY 和 GROUP_CONCAT()函数⼀起使⽤
mysql> select post,GROUP_CONCAT(name) from t2 GROUP BY post;

# 使用正则表达式查询
mysql> select * from t2 where name REGEXP '^ali';


标签:salary,name,数据库,SQL,t2,mysql,基本操作,where,select
From: https://www.cnblogs.com/louvice/p/17691755.html

相关文章

  • MySQL数据库进阶 自定义函数
    自定义函数在MySQL中,您可以使用自定义函数来扩展数据库管理系统的功能。自定义函数允许您封装一段可重用的代码,并在查询和其他操作中调用它。以下是在MySQL中创建和使用自定义函数的一般步骤:1、创建自定义函数语法:CREATEFUNCTIONfunction_name(parameters)RETURNSreturn_t......
  • 7种数据库的存在则更新,不存在则插入(UPSERT)SQL写法
    在工作中遇到了这么一个需求,在往数据库插入数据时,如果一条数据不存在则插入,如果存在(根据主键或唯一索引判断)则更新,也就是所谓的UPSERT操作。大部分数据库都没有UPSERT语句,不过他们都有一些SQL的写法来实现这样的操作。1.MySQL使用ONDUPLICATEKEYUPDATE,如果要插入这条数据将......
  • Navicat连接Docker创建的MySQL运行实例
    一步步教你在Docker中安装MySQL(qq.com)Docker部署MySql应用-魔鬼YU天使-博客园(cnblogs.com)拉取MySQL镜像我们可以通过Docker提供的命令,直接从DockerHub上拉取MySQL的官方镜像。打开终端,输入如下命令:dockerpullmysql:8.0其中,:8.0表示我们需要下载的MySQL版本,你也......
  • linux7.X二进制方式安装MySQL5.7.X
    概述系统环境服务器型号VMwareVirtualPlatform操作系统版本RHEL7.6x86_64主机名linuxpg51PubIP192.168.115.51数据库安装版本mysqlVer14.14Distrib5.7.30,forlinux-glibc2.12(x86_64)usingEditLinewrapper安装路径规划[mysql@linuxpg51/]$tree-L1/mysql/mysq......
  • Java实现关系型数据库工具类JdbcUtils系列九:通用DAO
    Java实现关系型数据库工具类JdbcUtils系列九:通用DAO一、创建对应数据库表的实体类二、数据库连接池Druid工具类三、DAO类四、BaseDAO五、DatabaseInfoDao六、通用DAO测试类一、创建对应数据库表的实体类数据库表结构CREATETABLE`databaseInfo`(`id`bigint(11)NOTNULLAU......
  • 数据库连接池Druid使用方法
    数据库连接池Druid使用方法一、Druid连接池使用代码示例importcom.alibaba.druid.pool.DruidAbstractDataSource;importcom.alibaba.druid.pool.DruidDataSource;importcom.alibaba.druid.pool.DruidDataSourceFactory;importorg.junit.Test;importjavax.sql.DataSource;......
  • Java应用程序中的数据库连接池优化
    什么是数据库连接池?数据库连接池是一种数据库连接的管理技术,它允许应用程序在需要时从池中获取数据库连接,而不是每次都创建新的连接。这样可以减少连接创建和销毁的开销,提高数据库访问性能。为什么需要数据库连接池?在Java应用程序中,频繁地创建和关闭数据库连接会导致性能下降,因为连......
  • MongoDB下载和可视化工具NoSQL Manager for MongoDB 软件的下载,连接数据库
      在官网下载MongoDB的版本为4.0.28,之前试了好几个高版本和低版本,都不行,最后,4.0.28版本好了。下载网页:https://www.mongodb.com/try/download/community 安装和配置环境变量的方法一搜就行了,不难。打开安装MongoDB的bin路径,输入cmd,打开命令提示符,再输入红色圈住的命令,看是......
  • 实现数据库连接池druid的工具类
    一、数据库连接迟druid工具类importcom.alibaba.druid.pool.DruidDataSourceFactory;importorg.apache.commons.beanutils.PropertyUtils;importjavax.sql.DataSource;importjava.io.IOException;importjava.sql.*;importjava.util.ArrayList;importjava.util.List;......
  • MySQL基础
    要学习数据库首先要先搞清楚三个概念数据库(DB):是存储数据的仓库数据库管理系统(DBMS):管理数据库的大型软件SQL:通过SQL操作数据库管理系统操作数据库,对数据库进行增删改查等由此我们可以知道数据库就是安装在操作系统之上的数据仓库,用于存储数据。我们也先认识一个概念->关系型数据......