mysql 相关链接与子查询的练习,以及索引视图的简单概述

4月1日 索引与视图

一 销售查询问题复习链接和子查询

1)子查询相关sql语句

-- 结果返回一个值
select 查询字段 from 表 where 字段 [= > < <>] (子查询的内容)-- 单列多行
select 查询字段 from 表 where 字段 in (子查询)-- 多列多行
select 查询字段 from (子查询) 表别名 where 条件

2)案例如下

-- 用户表
use sys;create table `user` (                                  `id` int auto_increment primary key,                `username` varchar(50)  -- 用户姓名                                                );insert into user values(3,'张三');insert into user values(4,'李四');insert into user values(5,'王五');insert into user values(6,'赵六');select * from user;-- 订单表use sys;create table `orders` (                                                  `id` int  auto_increment primary key,                                  `price` double,                                           `user_id` int                                       );insert into orders values(1,1314,3);insert into orders values(2,1314,3);insert into orders values(3,15,4);insert into orders values(4,315,5);insert into orders values(5,1014,null);select * from orders;

问题练习如下:

-- 1) 查询已经购买商品的用户情况
-- 方法1 inner join
select * from user t1 inner join orders t2 on t1.id=t2.user_id;
-- 方法2 子查询
select * from user u, orders o where u.id=o.user_id;-- 2) 查看所有销售情况
-- 方法1 left join 
select * from user t1 left join orders t2 on t1.id=t2.user_id;
-- 如果想筛选查询内容,例如我只想看到第一个表的用户名,就把想看的打出来(username是我想看到的)
select t1.username, t2.* from user t1 left join orders t2 on t1.id=t2.user_id;-- 3) 查询所有订单的用户详情
-- 方法1 还是左链接只是把表换个位置(商品信息表放左边)
select * from orders t2 left join user t1 on t1.id=t2.user_id;
-- 方法2 right join
select * from user t1 right join orders t2 on ti.id=t2.user_id;-- 4) 查询订单价格大于300的用户信息
select * from orders t2 left join user t1 on t1.id=t2.user_id where price > 300;
-- 方法2 多行子查询
select * from user where id in(select user_id from orders where price > 300);-- 5) 查询价格大于300的订单, 以及相关用户的信息
select * from user t1 inner join orders t2 on t1.id=t2.user_id where price > 300;
-- 方法2 join...on 的用法
select * from user u join (select * from orders where price > 300) o on u.id = o.user_id;
-- (select * from orders where price > 300) o 相当于orders o
-- join用于根据2个或多个表中的列之间的关系,获取存在于不同表中的数据,具有连接作用。
-- on关键字用于设置匹配条件,用于限定在结果集合中想要哪些行

二 工资查询

案例如下:

/*!40101 SET NAMES utf8 */;/*!40101 SET SQL_MODE=''*/;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`myemployees` /*!40100 DEFAULT CHARACTER SET gb2312 */;USE `myemployees`;/*Table structure for table `departments` */DROP TABLE IF EXISTS `departments`;CREATE TABLE `departments` (`department_id` int(4) NOT NULL AUTO_INCREMENT,`department_name` varchar(3) DEFAULT NULL,`manager_id` int(6) DEFAULT NULL,`location_id` int(4) DEFAULT NULL,PRIMARY KEY (`department_id`),KEY `loc_id_fk` (`location_id`),CONSTRAINT `loc_id_fk` FOREIGN KEY (`location_id`) REFERENCES `locations` (`location_id`)
) ENGINE=InnoDB AUTO_INCREMENT=271 DEFAULT CHARSET=gb2312;
select * from departments;/*Data for the table `departments` */insert  into `departments`(`department_id`,`department_name`,`manager_id`,`location_id`) values (10,'Adm',200,1700),(20,'Mar',201,1800),(30,'Pur',114,1700),(40,'Hum',203,2400),(50,'Shi',121,1500),(60,'IT',103,1400),(70,'Pub',204,2700),(80,'Sal',145,2500),(90,'Exe',100,1700),(100,'Fin',108,1700),(110,'Acc',205,1700),(120,'Tre',NULL,1700),(130,'Cor',NULL,1700),(140,'Con',NULL,1700),(150,'Sha',NULL,1700),(160,'Ben',NULL,1700),(170,'Man',NULL,1700),(180,'Con',NULL,1700),(190,'Con',NULL,1700),(200,'Ope',NULL,1700),(210,'IT ',NULL,1700),(220,'NOC',NULL,1700),(230,'IT ',NULL,1700),(240,'Gov',NULL,1700),(250,'Ret',NULL,1700),(260,'Rec',NULL,1700),(270,'Pay',NULL,1700);/*Table structure for table `employees` */DROP TABLE IF EXISTS `employees`;CREATE TABLE `employees` (`employee_id` int(6) NOT NULL AUTO_INCREMENT,`first_name` varchar(20) DEFAULT NULL,`last_name` varchar(25) DEFAULT NULL,`email` varchar(25) DEFAULT NULL,`phone_number` varchar(20) DEFAULT NULL,`job_id` varchar(10) DEFAULT NULL,`salary` double(10,2) DEFAULT NULL,`commission_pct` double(4,2) DEFAULT NULL,`manager_id` int(6) DEFAULT NULL,`department_id` int(4) DEFAULT NULL,`hiredate` datetime DEFAULT NULL,PRIMARY KEY (`employee_id`),KEY `dept_id_fk` (`department_id`),KEY `job_id_fk` (`job_id`),CONSTRAINT `dept_id_fk` FOREIGN KEY (`department_id`) REFERENCES `departments` (`department_id`),CONSTRAINT `job_id_fk` FOREIGN KEY (`job_id`) REFERENCES `jobs` (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=207 DEFAULT CHARSET=gb2312;/*Data for the table `employees` */insert  into `employees`(`employee_id`,`first_name`,`last_name`,`email`,`phone_number`,`job_id`,`salary`,`commission_pct`,`manager_id`,`department_id`,`hiredate`) values (100,'Steven','K_ing','SKING','515.123.4567','AD_PRES',24000.00,NULL,NULL,90,'1992-04-03 00:00:00'),(101,'Neena','Kochhar','NKOCHHAR','515.123.4568','AD_VP',17000.00,NULL,100,90,'1992-04-03 00:00:00'),(102,'Lex','De Haan','LDEHAAN','515.123.4569','AD_VP',17000.00,NULL,100,90,'1992-04-03 00:00:00'),(103,'Alexander','Hunold','AHUNOLD','590.423.4567','IT_PROG',9000.00,NULL,102,60,'1992-04-03 00:00:00'),(104,'Bruce','Ernst','BERNST','590.423.4568','IT_PROG',6000.00,NULL,103,60,'1992-04-03 00:00:00'),(105,'David','Austin','DAUSTIN','590.423.4569','IT_PROG',4800.00,NULL,103,60,'1998-03-03 00:00:00'),(106,'Valli','Pataballa','VPATABAL','590.423.4560','IT_PROG',4800.00,NULL,103,60,'1998-03-03 00:00:00'),(107,'Diana','Lorentz','DLORENTZ','590.423.5567','IT_PROG',4200.00,NULL,103,60,'1998-03-03 00:00:00'),(108,'Nancy','Greenberg','NGREENBE','515.124.4569','FI_MGR',12000.00,NULL,101,100,'1998-03-03 00:00:00'),(109,'Daniel','Faviet','DFAVIET','515.124.4169','FI_ACCOUNT',9000.00,NULL,108,100,'1998-03-03 00:00:00'),(110,'John','Chen','JCHEN','515.124.4269','FI_ACCOUNT',8200.00,NULL,108,100,'2000-09-09 00:00:00'),(111,'Ismael','Sciarra','ISCIARRA','515.124.4369','FI_ACCOUNT',7700.00,NULL,108,100,'2000-09-09 00:00:00'),(112,'Jose Manuel','Urman','JMURMAN','515.124.4469','FI_ACCOUNT',7800.00,NULL,108,100,'2000-09-09 00:00:00'),(113,'Luis','Popp','LPOPP','515.124.4567','FI_ACCOUNT',6900.00,NULL,108,100,'2000-09-09 00:00:00'),(114,'Den','Raphaely','DRAPHEAL','515.127.4561','PU_MAN',11000.00,NULL,100,30,'2000-09-09 00:00:00'),(115,'Alexander','Khoo','AKHOO','515.127.4562','PU_CLERK',3100.00,NULL,114,30,'2000-09-09 00:00:00'),(116,'Shelli','Baida','SBAIDA','515.127.4563','PU_CLERK',2900.00,NULL,114,30,'2000-09-09 00:00:00'),(117,'Sigal','Tobias','STOBIAS','515.127.4564','PU_CLERK',2800.00,NULL,114,30,'2000-09-09 00:00:00'),(118,'Guy','Himuro','GHIMURO','515.127.4565','PU_CLERK',2600.00,NULL,114,30,'2000-09-09 00:00:00'),(119,'Karen','Colmenares','KCOLMENA','515.127.4566','PU_CLERK',2500.00,NULL,114,30,'2000-09-09 00:00:00'),(120,'Matthew','Weiss','MWEISS','650.123.1234','ST_MAN',8000.00,NULL,100,50,'2004-02-06 00:00:00'),(121,'Adam','Fripp','AFRIPP','650.123.2234','ST_MAN',8200.00,NULL,100,50,'2004-02-06 00:00:00'),(122,'Payam','Kaufling','PKAUFLIN','650.123.3234','ST_MAN',7900.00,NULL,100,50,'2004-02-06 00:00:00'),(123,'Shanta','Vollman','SVOLLMAN','650.123.4234','ST_MAN',6500.00,NULL,100,50,'2004-02-06 00:00:00'),(124,'Kevin','Mourgos','KMOURGOS','650.123.5234','ST_MAN',5800.00,NULL,100,50,'2004-02-06 00:00:00'),(125,'Julia','Nayer','JNAYER','650.124.1214','ST_CLERK',3200.00,NULL,120,50,'2004-02-06 00:00:00'),(126,'Irene','Mikkilineni','IMIKKILI','650.124.1224','ST_CLERK',2700.00,NULL,120,50,'2004-02-06 00:00:00'),(127,'James','Landry','JLANDRY','650.124.1334','ST_CLERK',2400.00,NULL,120,50,'2004-02-06 00:00:00'),(128,'Steven','Markle','SMARKLE','650.124.1434','ST_CLERK',2200.00,NULL,120,50,'2004-02-06 00:00:00'),(129,'Laura','Bissot','LBISSOT','650.124.5234','ST_CLERK',3300.00,NULL,121,50,'2004-02-06 00:00:00'),(130,'Mozhe','Atkinson','MATKINSO','650.124.6234','ST_CLERK',2800.00,NULL,121,50,'2004-02-06 00:00:00'),(131,'James','Marlow','JAMRLOW','650.124.7234','ST_CLERK',2500.00,NULL,121,50,'2004-02-06 00:00:00'),(132,'TJ','Olson','TJOLSON','650.124.8234','ST_CLERK',2100.00,NULL,121,50,'2004-02-06 00:00:00'),(133,'Jason','Mallin','JMALLIN','650.127.1934','ST_CLERK',3300.00,NULL,122,50,'2004-02-06 00:00:00'),(134,'Michael','Rogers','MROGERS','650.127.1834','ST_CLERK',2900.00,NULL,122,50,'2002-12-23 00:00:00'),(135,'Ki','Gee','KGEE','650.127.1734','ST_CLERK',2400.00,NULL,122,50,'2002-12-23 00:00:00'),(136,'Hazel','Philtanker','HPHILTAN','650.127.1634','ST_CLERK',2200.00,NULL,122,50,'2002-12-23 00:00:00'),(137,'Renske','Ladwig','RLADWIG','650.121.1234','ST_CLERK',3600.00,NULL,123,50,'2002-12-23 00:00:00'),(138,'Stephen','Stiles','SSTILES','650.121.2034','ST_CLERK',3200.00,NULL,123,50,'2002-12-23 00:00:00'),(139,'John','Seo','JSEO','650.121.2019','ST_CLERK',2700.00,NULL,123,50,'2002-12-23 00:00:00'),(140,'Joshua','Patel','JPATEL','650.121.1834','ST_CLERK',2500.00,NULL,123,50,'2002-12-23 00:00:00'),(141,'Trenna','Rajs','TRAJS','650.121.8009','ST_CLERK',3500.00,NULL,124,50,'2002-12-23 00:00:00'),(142,'Curtis','Davies','CDAVIES','650.121.2994','ST_CLERK',3100.00,NULL,124,50,'2002-12-23 00:00:00'),(143,'Randall','Matos','RMATOS','650.121.2874','ST_CLERK',2600.00,NULL,124,50,'2002-12-23 00:00:00'),(144,'Peter','Vargas','PVARGAS','650.121.2004','ST_CLERK',2500.00,NULL,124,50,'2002-12-23 00:00:00'),(145,'John','Russell','JRUSSEL','011.44.1344.429268','SA_MAN',14000.00,0.40,100,80,'2002-12-23 00:00:00'),(146,'Karen','Partners','KPARTNER','011.44.1344.467268','SA_MAN',13500.00,0.30,100,80,'2002-12-23 00:00:00'),(147,'Alberto','Errazuriz','AERRAZUR','011.44.1344.429278','SA_MAN',12000.00,0.30,100,80,'2002-12-23 00:00:00'),(148,'Gerald','Cambrault','GCAMBRAU','011.44.1344.619268','SA_MAN',11000.00,0.30,100,80,'2002-12-23 00:00:00'),(149,'Eleni','Zlotkey','EZLOTKEY','011.44.1344.429018','SA_MAN',10500.00,0.20,100,80,'2002-12-23 00:00:00'),(150,'Peter','Tucker','PTUCKER','011.44.1344.129268','SA_REP',10000.00,0.30,145,80,'2014-03-05 00:00:00'),(151,'David','Bernstein','DBERNSTE','011.44.1344.345268','SA_REP',9500.00,0.25,145,80,'2014-03-05 00:00:00'),(152,'Peter','Hall','PHALL','011.44.1344.478968','SA_REP',9000.00,0.25,145,80,'2014-03-05 00:00:00'),(153,'Christopher','Olsen','COLSEN','011.44.1344.498718','SA_REP',8000.00,0.20,145,80,'2014-03-05 00:00:00'),(154,'Nanette','Cambrault','NCAMBRAU','011.44.1344.987668','SA_REP',7500.00,0.20,145,80,'2014-03-05 00:00:00'),(155,'Oliver','Tuvault','OTUVAULT','011.44.1344.486508','SA_REP',7000.00,0.15,145,80,'2014-03-05 00:00:00'),(156,'Janette','K_ing','JKING','011.44.1345.429268','SA_REP',10000.00,0.35,146,80,'2014-03-05 00:00:00'),(157,'Patrick','Sully','PSULLY','011.44.1345.929268','SA_REP',9500.00,0.35,146,80,'2014-03-05 00:00:00'),(158,'Allan','McEwen','AMCEWEN','011.44.1345.829268','SA_REP',9000.00,0.35,146,80,'2014-03-05 00:00:00'),(159,'Lindsey','Smith','LSMITH','011.44.1345.729268','SA_REP',8000.00,0.30,146,80,'2014-03-05 00:00:00'),(160,'Louise','Doran','LDORAN','011.44.1345.629268','SA_REP',7500.00,0.30,146,80,'2014-03-05 00:00:00'),(161,'Sarath','Sewall','SSEWALL','011.44.1345.529268','SA_REP',7000.00,0.25,146,80,'2014-03-05 00:00:00'),(162,'Clara','Vishney','CVISHNEY','011.44.1346.129268','SA_REP',10500.00,0.25,147,80,'2014-03-05 00:00:00'),(163,'Danielle','Greene','DGREENE','011.44.1346.229268','SA_REP',9500.00,0.15,147,80,'2014-03-05 00:00:00'),(164,'Mattea','Marvins','MMARVINS','011.44.1346.329268','SA_REP',7200.00,0.10,147,80,'2014-03-05 00:00:00'),(165,'David','Lee','DLEE','011.44.1346.529268','SA_REP',6800.00,0.10,147,80,'2014-03-05 00:00:00'),(166,'Sundar','Ande','SANDE','011.44.1346.629268','SA_REP',6400.00,0.10,147,80,'2014-03-05 00:00:00'),(167,'Amit','Banda','ABANDA','011.44.1346.729268','SA_REP',6200.00,0.10,147,80,'2014-03-05 00:00:00'),(168,'Lisa','Ozer','LOZER','011.44.1343.929268','SA_REP',11500.00,0.25,148,80,'2014-03-05 00:00:00'),(169,'Harrison','Bloom','HBLOOM','011.44.1343.829268','SA_REP',10000.00,0.20,148,80,'2014-03-05 00:00:00'),(170,'Tayler','Fox','TFOX','011.44.1343.729268','SA_REP',9600.00,0.20,148,80,'2014-03-05 00:00:00'),(171,'William','Smith','WSMITH','011.44.1343.629268','SA_REP',7400.00,0.15,148,80,'2014-03-05 00:00:00'),(172,'Elizabeth','Bates','EBATES','011.44.1343.529268','SA_REP',7300.00,0.15,148,80,'2014-03-05 00:00:00'),(173,'Sundita','Kumar','SKUMAR','011.44.1343.329268','SA_REP',6100.00,0.10,148,80,'2014-03-05 00:00:00'),(174,'Ellen','Abel','EABEL','011.44.1644.429267','SA_REP',11000.00,0.30,149,80,'2014-03-05 00:00:00'),(175,'Alyssa','Hutton','AHUTTON','011.44.1644.429266','SA_REP',8800.00,0.25,149,80,'2014-03-05 00:00:00'),(176,'Jonathon','Taylor','JTAYLOR','011.44.1644.429265','SA_REP',8600.00,0.20,149,80,'2014-03-05 00:00:00'),(177,'Jack','Livingston','JLIVINGS','011.44.1644.429264','SA_REP',8400.00,0.20,149,80,'2014-03-05 00:00:00'),(178,'Kimberely','Grant','KGRANT','011.44.1644.429263','SA_REP',7000.00,0.15,149,NULL,'2014-03-05 00:00:00'),(179,'Charles','Johnson','CJOHNSON','011.44.1644.429262','SA_REP',6200.00,0.10,149,80,'2014-03-05 00:00:00'),(180,'Winston','Taylor','WTAYLOR','650.507.9876','SH_CLERK',3200.00,NULL,120,50,'2014-03-05 00:00:00'),(181,'Jean','Fleaur','JFLEAUR','650.507.9877','SH_CLERK',3100.00,NULL,120,50,'2014-03-05 00:00:00'),(182,'Martha','Sullivan','MSULLIVA','650.507.9878','SH_CLERK',2500.00,NULL,120,50,'2014-03-05 00:00:00'),(183,'Girard','Geoni','GGEONI','650.507.9879','SH_CLERK',2800.00,NULL,120,50,'2014-03-05 00:00:00'),(184,'Nandita','Sarchand','NSARCHAN','650.509.1876','SH_CLERK',4200.00,NULL,121,50,'2014-03-05 00:00:00'),(185,'Alexis','Bull','ABULL','650.509.2876','SH_CLERK',4100.00,NULL,121,50,'2014-03-05 00:00:00'),(186,'Julia','Dellinger','JDELLING','650.509.3876','SH_CLERK',3400.00,NULL,121,50,'2014-03-05 00:00:00'),(187,'Anthony','Cabrio','ACABRIO','650.509.4876','SH_CLERK',3000.00,NULL,121,50,'2014-03-05 00:00:00'),(188,'Kelly','Chung','KCHUNG','650.505.1876','SH_CLERK',3800.00,NULL,122,50,'2014-03-05 00:00:00'),(189,'Jennifer','Dilly','JDILLY','650.505.2876','SH_CLERK',3600.00,NULL,122,50,'2014-03-05 00:00:00'),(190,'Timothy','Gates','TGATES','650.505.3876','SH_CLERK',2900.00,NULL,122,50,'2014-03-05 00:00:00'),(191,'Randall','Perkins','RPERKINS','650.505.4876','SH_CLERK',2500.00,NULL,122,50,'2014-03-05 00:00:00'),(192,'Sarah','Bell','SBELL','650.501.1876','SH_CLERK',4000.00,NULL,123,50,'2014-03-05 00:00:00'),(193,'Britney','Everett','BEVERETT','650.501.2876','SH_CLERK',3900.00,NULL,123,50,'2014-03-05 00:00:00'),(194,'Samuel','McCain','SMCCAIN','650.501.3876','SH_CLERK',3200.00,NULL,123,50,'2014-03-05 00:00:00'),(195,'Vance','Jones','VJONES','650.501.4876','SH_CLERK',2800.00,NULL,123,50,'2014-03-05 00:00:00'),(196,'Alana','Walsh','AWALSH','650.507.9811','SH_CLERK',3100.00,NULL,124,50,'2014-03-05 00:00:00'),(197,'Kevin','Feeney','KFEENEY','650.507.9822','SH_CLERK',3000.00,NULL,124,50,'2014-03-05 00:00:00'),(198,'Donald','OConnell','DOCONNEL','650.507.9833','SH_CLERK',2600.00,NULL,124,50,'2014-03-05 00:00:00'),(199,'Douglas','Grant','DGRANT','650.507.9844','SH_CLERK',2600.00,NULL,124,50,'2014-03-05 00:00:00'),(200,'Jennifer','Whalen','JWHALEN','515.123.4444','AD_ASST',4400.00,NULL,101,10,'2016-03-03 00:00:00'),(201,'Michael','Hartstein','MHARTSTE','515.123.5555','MK_MAN',13000.00,NULL,100,20,'2016-03-03 00:00:00'),(202,'Pat','Fay','PFAY','603.123.6666','MK_REP',6000.00,NULL,201,20,'2016-03-03 00:00:00'),(203,'Susan','Mavris','SMAVRIS','515.123.7777','HR_REP',6500.00,NULL,101,40,'2016-03-03 00:00:00'),(204,'Hermann','Baer','HBAER','515.123.8888','PR_REP',10000.00,NULL,101,70,'2016-03-03 00:00:00'),(205,'Shelley','Higgins','SHIGGINS','515.123.8080','AC_MGR',12000.00,NULL,101,110,'2016-03-03 00:00:00'),(206,'William','Gietz','WGIETZ','515.123.8181','AC_ACCOUNT',8300.00,NULL,205,110,'2016-03-03 00:00:00');/*Table structure for table `jobs` */DROP TABLE IF EXISTS `jobs`;CREATE TABLE `jobs` (`job_id` varchar(10) NOT NULL,`job_title` varchar(35) DEFAULT NULL,`min_salary` int(6) DEFAULT NULL,`max_salary` int(6) DEFAULT NULL,PRIMARY KEY (`job_id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;/*Data for the table `jobs` */insert  into `jobs`(`job_id`,`job_title`,`min_salary`,`max_salary`) values ('AC_ACCOUNT','Public Accountant',4200,9000),('AC_MGR','Accounting Manager',8200,16000),('AD_ASST','Administration Assistant',3000,6000),('AD_PRES','President',20000,40000),('AD_VP','Administration Vice President',15000,30000),('FI_ACCOUNT','Accountant',4200,9000),('FI_MGR','Finance Manager',8200,16000),('HR_REP','Human Resources Representative',4000,9000),('IT_PROG','Programmer',4000,10000),('MK_MAN','Marketing Manager',9000,15000),('MK_REP','Marketing Representative',4000,9000),('PR_REP','Public Relations Representative',4500,10500),('PU_CLERK','Purchasing Clerk',2500,5500),('PU_MAN','Purchasing Manager',8000,15000),('SA_MAN','Sales Manager',10000,20000),('SA_REP','Sales Representative',6000,12000),('SH_CLERK','Shipping Clerk',2500,5500),('ST_CLERK','Stock Clerk',2000,5000),('ST_MAN','Stock Manager',5500,8500);
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
select * from departments; -- 部门表
select * from employees; -- 工资表
select * from jobs; -- 工作表

问题练习如下:

-- 一、查询部门平均工资最低的信息
select department_id from employees group by department_id order by avg(salary) limit 1;
-- 体验limit 用法
-- 当limit后面只有一个数字,则这个数字是什么就取第几行的数据 
select * from employees t1 inner join departments t2 on t1.manager_id=t2.department_id limit 1;
-- 当limit后面有2个数,eg.limit 1,2 就是从第2行开始,向后取2行的内容
select * from employees t1 inner join departments t2 on t1.manager_id=t2.department_id limit 1,2;
select  distinct E.* from employees E join employees d on e.employee_id = d.manager_id;-- 二、 查询工资最高或最底的信息:last_name,salary
select last_name,salary from employees where salary = (select min(salary) from employees);
select last_name from employees join(select salary from departments as salary) on  max(departments.location_id)-- 三、查询工资最高的jobs的ID
select job_id , avg(salary) pj from employees group by job_id order by pj desc limit 1;
-- 上面的语句相当于
select job_id , avg(salary) as pj from employees group by job_id order by pj desc limit 1;-- 四、工资最高的工作
select job_id , max_salary from jobs group by job_id order by max_salary desc limit 1;-- 五、查询公司中manager_id的相关信息
select * from employees t1 inner join departments t2 on t1.manager_id=t2.department_id; -- 内连接查询

三 索引于视图相关的概念以及操作

一)索引

1.什么是索引?索引分为哪几类?哪些适合建索引哪些不适合建索引。(知识点:索引的基本概念)

1)索引是一种数据结构,可以帮助快速查询数据库表中的特定记录。索引的创建可以提高查询性能,但也会影响插入、删除和更新操作的速度,因为在修改数据时索引也需要被更新。
2)普通索引、唯一索引、主键索引、空间索引、全文索引、单列索引、多行索引
3)频繁修改、唯一性差、长度较长、静态数据、不会在查询中使用等字段不适合当索引

2.查看某表(STUDENTS)已建有哪些索引。(知识点:SHOW INDEX FROM语法)

show index from students;

3.给STUDENTS表的SNAME字段创建普通索引STUINDEX1。(知识点:CREATE INDEX语法)

create index STUINDEX1 on students(sname);

4.给STUDENTS表创建联合索引STUINDEX2,SNAME字段前2字符升序,SNO字段降序。(知识点:ALTER TABLE …ADD INDEX语法)

create index studex2 on students(sname(2),sno desc);

5.删除索引

alter table students and index stuindex2;

6.删除数据表STUDENTS中的STUINDEX1索引。

alter table students drop index stuindex1

二)视图

1.什么是视图。(知识点:视图定义)

1)是一种虚拟表
2)视图建立在已有表的基础上,这些表被称为基表。
3)视图向用户提供基表数据的另一种表现形式,使用户能够更方便地查询和操作数据。
4)视图中的数据来自定义视图的查询所引用的表,在引用视图时动态生成。
5)视图可以简化复杂的多表查询,提高数据库安全性,并可以限定查询的列。

2.创建视图VW_DEPART_MAJOR:查询院部专业信息。

create view vw_depart_major as select `img` from table_name

3.创建STUDENTS表的视图vw_student_1,显示学号、姓名、班级名称、专业名称。

create view vw_depart_major as select id, sname,classname, majorname, schoolname, from students;

5.查看VW_STUDENTS_1视图信息。(知识点:DESCRIBE/SHOW CREATE VIEW语句)

describe vw_students_1 ;
-- 方法 2
show table like 'vw_students_1'

6.给VW_STUDENTS_1视图添加STUDENTS表的ID字段。(知识点:ALTER VIEW语句)

alter view vw_students_1 as select id from students;

7.删除VW_STUDENTS_1视图。(知识点:DROP VIEW语句)

drop view vw_students_1;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/789156.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

06 监听器

文章目录 SessionAttListenerDemo.javaSessionListenerDemo.javaProductController.java SessionAttListenerDemo.java package com.aistart.listener;import javax.servlet.ServletContext; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSess…

Tensorboard使用教程

Pytorch(九) —— Tensorboard(当有了tensorboard日志文件怎么可视化它)(同时显示多个模型)(vscode的tensorboard)(TensorboardX)_tensorboard --logdir-CSDN博客文章浏览阅读9.7k次&#xff0c;点赞10次&#xff0c;收藏56次。tensorboard.pyfrom tensorboardX import Summary…

NVIDIA Jetson Xavier NX增加swap分区大小

NVIDIA Jetson Xavier NX增加swap分区大小 1. 输入jtop查看swap空间大小 2. 依次输入命令 #1&#xff09;新增swapfile文件大小自定义 sudo fallocate -l 6G /var/swapfile #2&#xff09;配置该文件的权限 sudo chmod 600 /var/swapfile #3&#xff09;建立交换分区 sudo m…

最大子数组和-动态规划

53. 最大子数组和 问题&#xff1a;最大子数组和&#xff0c;找出数组中具有最大和的连续子数组。 思路&#xff1a; 暴力解法&#xff1a;通过两重循环遍历&#xff0c;得到所有子数组和。时间和空间复杂度都为O(n^2): class Solution {public int maxSubArray(int[] nums…

RuntimeError: Error compiling objects for extension虚拟环境和系统环境——添加、删除、修改环境变量

前言&#xff1a;因为一个报错RuntimeError: Error compiling objects for extension 没有配置cl.exe环境变量&#xff0c;我的应用场景是需要搞定虚拟环境变量配置 RuntimeError: Error compiling objects for extension手把手带你解决&#xff08;超详细&#xff09;-CSDN博…

爬虫 红网时刻 获取当月指定关键词新闻 并存储到CSV文件

目标网站&#xff1a;红网 爬取目的&#xff1a;为了获取某一地区更全面的在红网已发布的宣传新闻稿&#xff0c;同时也让自己的工作更便捷 环境&#xff1a;Pycharm2021&#xff0c;Python3.10&#xff0c; 安装的包&#xff1a;requests&#xff0c;csv&#xff0c;bs4&…

如何在pgAdmin中用替换的值更新jsonb列?(二)

上一篇提到怎么替换jsonb&#xff0c;链接如下&#xff1a; 如何在pgAdmin中用替换的值更新jsonb列&#xff1f;-CSDN博客 那么当jsonb嵌套jsonb应该怎么替换呢&#xff1f;像这样&#xff0c;类型依然是jsonb&#xff0c;只不过嵌套一层&#xff0c;JsonData&#xff1a;&qu…

网络安全 | 什么是DDoS攻击?

关注WX&#xff1a;CodingTechWork DDoS-介绍 DoS&#xff1a;Denial of Service&#xff0c;拒绝服务。DDoS是通过大规模的网络流量使得正常流量不能访问受害者目标&#xff0c;是一种压垮性的网络攻击&#xff0c;而不是一种入侵手段。NTP网络时间协议&#xff0c;设备需要…

Springboot整合Milvus向量库

1. Milvus的Maven依赖&#xff0c; 配置如下 <dependency><groupId>io.milvus</groupId><artifactId>milvus-sdk-java</artifactId><version>2.3.4</version><exclusions><exclusion><artifactId>log4j-slf4j-imp…

百度语音识别

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 一、建号—获取试用KEY二、测试代码三、运行四、运行结果五、验证五、总结 一、建号—获取试用KEY https://console.bce.baidu.com/ai/#/ai/speech/overview/index…

Tomcat部署及安全加固

一、安装步骤 tomcat版本(8.5.81)&#xff1a; tar -zxvf apache-tomcat-8.5.81.tar.gz -C /app/ cd /app/ mv apache-tomcat-8.5.81/ tomcat/ chown -R webapp:app /app/tomcat二、安全加固 1. 禁止默认页面登录 rm -rf /app/tomcat/webapps mkdir -p /app/tomcat/webapps2…

阿里云python-SDK配置

OSS管理控制台 (aliyun.com) SDK地址 GitHub - aliyun/aliyun-oss-python-sdk: Aliyun OSS SDK for Python 下载文件并运行 python setup.py install oss2包配置完成。 上传、下载开发文档&#xff1a; Python SDK快速入门_对象存储(OSS)-阿里云帮助中心 (aliyun.com)

mybatis-plus-join,一个可以让mybatis-plus实现联表的插件

mybatis-plus-join mybatis-plus-join是mybatis plus的一个多表插件&#xff0c;上手简单&#xff0c;十分钟不到就能学会全部使用方式&#xff0c;只要会用mp就会用这个插件&#xff0c;仅仅依赖了lombok&#xff0c;而且是扩展mp的构造器并非更改原本的构造器&#xff0c;不会…

记一次Cannot deploy POJO class [xxx$$EnhancerBySpringCGLIB$$xxx]的错误

最近项目上需要使用websocket做服务端&#xff0c;那好说啊&#xff0c;直接springboot集成的websocket 引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId><versi…

赛氪网在长沙宣布启动“徽文化外宣翻译大赛”

2024年3月30日下午&#xff0c;在美丽的星城长沙&#xff0c;赛氪网作为承办方&#xff0c;在中国翻译协会年会期间成功举办了备受瞩目的“AI科技时代竞赛与就业分论坛”。此次论坛汇聚了众多翻译界、科技界和教育界的专家学者&#xff0c;共同就科技、实践、就业与竞赛人才培养…

一些关于机器学习的练习

为了加深机器学习基础并通过实践学习&#xff0c;收集了以下练习&#xff0c;覆盖了从数据探索、预处理到模型构建和评估的整个流程。这些练习按照机器学习的主要类型分为三部分&#xff1a;监督学习、无监督学习和强化学习。 监督学习练习 房价预测&#xff08;线性回归&…

使用C++调用讯飞星火API的详细指南

正文&#xff1a; 科大讯飞是中国领先的人工智能公司&#xff0c;其讯飞星火API为开发者提供了丰富的接口和服务&#xff0c;支持各种语音和语言技术的应用。下面是使用C接入讯飞星火API的步骤和代码示例。 步骤一&#xff1a;注册账号并创建应用 首先&#xff0c;您需要访问科…

蓝色wordpress外贸建站模板

蓝色wordpress外贸建站模板 https://www.mymoban.com/wordpress/7.html

ES 7.12官网阅读-ILM(index lifecycle management)

官网文档&#xff1a;ILM: Manage the index lifecycle | Elasticsearch Guide [7.12] | Elastic ILM&#xff1a;管理 index 的生命周期 可以根据你的性能、弹性、保存时长需求&#xff0c;使用ILM策略来自动管理你的index&#xff1b;比如 1. 当一个index达到确定的大小&a…

保护你的 Java 代码:深入了解代码混淆

在当今数字化时代&#xff0c;软件开发领域竞争激烈&#xff0c;而保护你的代码免受恶意攻击和盗用是至关重要的。代码混淆是一种常用的技术&#xff0c;用于增加攻击者分析和逆向工程代码的难度&#xff0c;从而提高代码的安全性。本文将介绍代码混淆的基本概念和详细办法&…