MySQL之DML(Data Manipulation Languages)

为了方便执行语句,这里在MySQL Workbench上进行数据库连接操作。在上一篇创建的user表进行操作,user_id字段使用UUID作为唯一键,可以用mysql的UUID函数生成:

1. 增

INSERT INTO iHouse.user(user_name, user_id, last_login_date, email_addr) VALUES ('LinJK', '758259ad-04cb-11e7-9d4a-52540036b0ab', '2017-03-09', 'test@localhost.com');

可以同时插入多条数据,在values后面用逗号分隔需要插入的多条数据即可。

再次执行该语句,报如下错误,说明UUID一样,唯一键可以防止数据重复插入:

2. 删

DELETE FROM iHouse.user where id=4;

3. 改

UPDATE iHouse.user set email_addr='haha@localhost.com' where id=3;

4. 查

SELECT * FROM iHouse.user;

SELECT * FROM iHouse.user where id=3 and user_name='LinJK';

插入部分数据后:

去重查询(邮件重复的去掉):

SELECT DISTINCT email_addr FROM iHouse.user;

排序查询(DESC降序  ASC升序)

SELECT * FROM iHouse.user order by id desc;

也可使用limit关键字控制输出结果数量


查询记录数:

SELECT COUNT(*) from iHouse.user; 或 SELECT COUNT(1) from iHouse.user;

根据某个字段查询统计每个字段的数量:

SELECT email_addr, COUNT(1) FROM iHouse.user GROUP by email_addr;

根据某个字段查询统计每个字段的数量,再汇总总数:

SELECT email_addr, COUNT(1) FROM iHouse.user GROUP by email_addr with rollup;


根据某个字段查询统计每个字段的数量,统计出数量大于1的:

SELECT email_addr, COUNT(1) FROM iHouse.user GROUP by email_addr having count(1)>1;

另外还有连接查询和子查询,这里只有一个表就暂时不测试了,通常,表连接在很多情况下用于优化子查询,还有可以两个表数据查询后一起展示,这会用到union或union all关键字。

Table of Contents Course Description...................................................................................................................... vi Prerequisites ...............................................................................................................................vii Chapter 1 Introduction .......................................................................................... 1-1 1.1 Introduction of Course Topics..........................................................................................1-3 1.2 Measuring Efficiencies ....................................................................................................1-5 1.3 SAS Processing..............................................................................................................1-19 1.4 Controlling Memory and I/O Resources........................................................................1-23 1.5 Solutions to Exercises....................................................................................................1-37 Chapter 2 Accessing Observations...................................................................... 2-1 2.1 Introduction......................................................................................................................2-3 2.2 Creating a Sample Data Set .............................................................................................2-7 2.3 Creating and Using an Index..........................................................................................2-36 2.4 Solutions to Exercises....................................................................................................2-71 Chapter 3 Combining Data Horizontally............................................................... 3-1 3.1 Joining Data Sets by Value ..............................................................................................3-3 3.2 Combining Summary and Detail Data...........................................................................3-37 3.3 Using an Index to Combine Data...................................................................................3-56 3.4 Updating Data ................................................................................................................3-72 3.5 Combining Summary and Detail Data Using Two SET Statements (Self-Study) .........3-93 3.6 Solutions to Exercises..................................................................................................3-106 iv For Your Information Chapter 4 Using Lookup Tables to Match Data................................................... 4-1 4.1 Introduction to Lookup Techniques .................................................................................4-3 4.2 Using Arrays as Lookup Tables .......................................................................................4-6 4.3 Using Hash Objects as Lookup Tables...........................................................................4-43 4.4 Using Formats as Lookup Tables...................................................................................4-77 4.5 Transposing Data to Create a Lookup Table ...............................................................4-108 4.6 Solutions to Exercises..................................................................................................4-119 Chapter 5 Combining Data Vertically ................................................................... 5-1 5.1 Appending SAS Data Sets ...............................................................................................5-3 5.2 Appending Raw Data Files ............................................................................................5-26 5.3 Solutions to Exercises....................................................................................................5-52 Chapter 6 BY-Group Processing and Sorting ..................................................... 6-1 6.1 Introduction......................................................................................................................6-3 6.2 Eliminating Duplicates.....................................................................................................6-5 6.3 Sorting Resources ..........................................................................................................6-16 6.4 Choosing the Right Sort Routine (Self-Study) ..............................................................6-31 6.5 Alternatives to Sorting ...................................................................................................6-37 6.6 Solutions to Exercises....................................................................................................6-65 Chapter 7 Controlling Data Storage Space.......................................................... 7-1 7.1 Introduction......................................................................................................................7-3 7.2 Reducing the Length of Numeric Variables.....................................................................7-6 7.3 Compressing Data Files .................................................................................................7-14 7.4 Creating a DATA Step View...........................................................................................7-28 For Your Information v 7.5 Solutions to Exercises....................................................................................................7-43 Chapter 8 Utilizing Best Practices to Improve Efficiency .................................. 8-1 8.1 Introduction......................................................................................................................8-3 8.2 Executing Only Necessary Statements ............................................................................8-7 8.3 Eliminating Unnecessary Passes through the Data ........................................................8-14 8.4 Reading and Writing Only Essential Data .....................................................................8-20 8.5 Networking Efficiency Considerations (Self-Study) .....................................................8-34 Chapter 9 Using the Scalable Performance Data Engine (Self-Study).............. 9-1 9.1 Introduction to the Scalable Performance Data Engine ...................................................9-3 9.2 Creating SPD Engine Tables..........................................................................................9-10 9.3 Using the SPD Engine Efficiently .................................................................................9-23 9.4 SPD Engine LIBNAME Statement Options List...........................................................9-28 Chapter 10 Additional Topics (Self-Study)........................................................... 10-1 10.1 Modifying SAS Data Sets in Place ................................................................................10-3 10.2 Creating Generation Data Sets.....................................................................................10-29 10.3 Creating Integrity Constraints......................................................................................10-50 10.4 Creating and Using Audit Trails ..................................................................................10-69 10.5 Working with Perl Regular Expressions ......................................................................10-81 10.6 Solutions to Exercises..................................................................................................10-97 Appendix A Index ..................................................................................................... A-1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值