You are on page 1of 45

Generated by Foxit PDF Creator Foxit Software

http://www.foxitsoftware.com For evaluation only.

SQL

.
.

................................................................................................................... 2
................................................................................................................... 2
2.1
Select ........................................................................................................ 2
2.2
Select distinct ........................................................................................... 3
2.3
Select as ...................................................... 3
2.4
SELECT INTO x,y ..................................................................................... 4
2.5
Where ....................................................................................................... 5
2.6
AND & OR ............................................................................................. 5
2.7
Group by .................................................................................................. 6
2.8
Having ...................................................................................................... 7
2.9
Order by ................................................................................................... 8
2.10
Insert into ................................................................................................. 9
2.11
Update .................................................................................................... 10
2.12
Delete ..................................................................................................... 11
2.13
Alter table ............................................................................................... 12
2.14
Drop ........................................................................................................ 13
2.15
Create table ............................................................................................ 15
.
................................................................................................................. 15
3.1
..................................................................... 15
3.2
............................................................................................. 16
3.3
................................................................................. 18
3.4
............................................................. 20
3.5
Inoutinout ........................................................... 21
3.6
...................................................................... 26
3.7
................................................................................. 29
3.8
................................................................................................. 35
.
......................................................................................................................... 39
4.1
..................................................................................................... 39
4.2
..................................................................................................... 39
4.3
................................................................................................. 40
4.4
EXPLAIN .................................................................................................. 40
4.5
..................................................................................................... 41
.
......................................................................................................................... 43
5.1
..................................................................................................... 43
5.2
............................................................................................. 43
5.3
..................................................................................... 44
5.4
......................................................................................................... 44

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

.
SQL SQL

SQL
ACCESS mysql

.
2.1 Select
select

Select from select *

ACCESS

ACCESS
SQL

Select *
From

ACCESS

from
2

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2.2 Select distinct

SQL

SQL

2.3 Select

as

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

SQL

78.764705 avg()int(avg())

2.4 SELECT INTO x,y

Select 1 2into x,y


From table_name
Where

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2.5 Where
select

SQL

2.6 AND & OR


where

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

where (=""and ="")or(=""and ="");

2.7 Group by
group

101 111 ;

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

SQL

101 111

101 111

2.8 Having
where int(avg())75 having
, group by
101 111 >80
having
SQL
7

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

having

having

2.9 Order by
order by desc

order by

SQL

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2.10 Insert into

Insert into value 1 2

Insert into 1 2 value 1 2

SQL

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2.11 Update

Update
Set =
Where
1999-1-2

SQL

1999-1-2

1999-1-2

10

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2.12 Delete
Delete :
Delete from where where

SQL

11

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2.13 Alter table

Alter table
Add

SQL

255

12

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2.14 Drop
alter table
Alter table
Drop column
;

13

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

drop table

14

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2.15 Create table

Create table 1 , 2 ,,:


ACCESS SQL

:name_1 sex ,

.
SQL ,
, execute,

ACCESS MYSQL

3.1
1

CREATE PROCEDURE p();


BEGIN
/**/
END
p ()

3.3
2
Call procedure sp_name();

3:
Drop procedure procedure_name
15

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

4
Show procedure status
;

3.2

DELIMITER $$
CREATE

/*[DEFINER = { user | CURRENT_USER }]*/


PROCEDURE `test`.`procedure_name`()
procedure_name

/*LANGUAGE SQL
SQL
| [NOT] DETERMINISTIC

| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
SQL
| SQL SECURITY { DEFINER | INVOKER }
SQL
| COMMENT 'string'*/

SQL

BEGIN
..
END$$
DELIMITER ;

MYSQL
begin and SQL ,

16

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

testabismdt MR_TA=26 MR_DL_RXQUAL_ALL=7

SQL
Select *
From testabismdt
Where MR_TA=26 and MR_DL_RXQUAL_ALL=7;
,

17

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

BEGIN SQL
begin end abcd ,
abcd

Call procedure_name ();


procedure_name;

call abcd();

3.3
create procedure ()
(
[in|out|inout] datatype
)
begin
MySQL ;
end;
3 inout inoutin
out inout
in
MySQL

18

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

a+b=c a b

,a int,b
int;
create procedure ()
( [in|out|inout] datatype)

begin END SQL C=a+b


SQL
sumabc

19

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

call sumabc3,8
sum=11

3.4
:BEGIN END
BEGIN

END

Label begin

END Label

2
While

20

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

Loop

3.5 Inoutinout
3.3 inoutinout

In:
Out:
Inout:

3.5.1

IN :

bbbba=a+1;10,11;

21

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

a=10, 10
a=10 IN IN a int

3.5.2

OUT :

adad A

22

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

a 10 1 OUT
a

3.5.3

Inout

bbbb A

23

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

inout
INOUT

24

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

3.5.4

MySQL

DECLARE variable_name [,variable_name...] datatype [DEFAULT value];


Declare
Variable_name,
Daatype
declared a int ;
int, INT declared a int default 6;
null;
Set =5

25

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

a b call ccc();
,

3.6
,

26

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

3.6.1 ()
DECLARE cursor_name CURSOR FOR SELECT_statement
Cursor_name :
SELECT_statementselect ,,
:
OPEN cursor_name;

FETCH cursor_name into


,
, FETCH

Close cursor_name;

3.6.2
select

Repeat loop while if

3.6.3

DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopFlag=1;


Set stopflag=0

3.6.4

27

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2,

4
5

DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `dfd`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `dfd`(database_name V
ARCHAR(255),tablename VARCHAR(255))
BEGIN
DECLARE cc VARCHAR(255);
DECLARE str VARCHAR(255);
DECLARE stopFlag INT;

DECLARE name_cursor CURSOR FOR


SELECT table_name
FROM information_schema.tables
WHERE table_schema=database_name;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET stopFlag=1;


SET stopFlag=0;
1
OPEN name_cursor;
REPEAT
FETCH name_cursor INTO cc;
INSTR

IF INSTR(cc,tablename)>0 THEN

SET @str=CONCAT('drop table if exists ',cc);


PREPARE sss FROM @str;

EXECUTE sss;
END IF;

UNTIL stopFlag=1
END REPEAT;

28

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

CLOSE name_cursor;
END$$
DELIMITER ;

3.7

3.7.1 Whie
While
[begin_label:] While search_condition do
statement_list
END WHILE [end_label]
while
search_condition While statement_list

while 100 i s
100 i 100 s i i 1
i 100

29

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

3.7.2 Loop

leave

loop

end loop
100

30

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

3.7.3 Repeat
condition

repeat

until condition end repeat;


100

31

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

3.7.4 ifthenelse if thenelse


if then
else if else

if condition then

[else if condition then]

[else]

end if;
1 1
2 2 3

32

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

33

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

3.7.5 Case
when value case value
else case
case value
when value then
[when value then]
[else]
end case
1 1
2 2 3

1 2 3

34

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

3.8
1
2.5.CSV

2 SQl 2.5
3.csv

DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `success`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `success`(ci VARCHAR(20),rxqual_level
VARCHAR(20))
BEGIN
SELECT callid,AVG(MR_DL_RXQUAL),AVG(MR_DL_RXLEV) INTO OUTFILE
"C:/result.csv"
.CSV
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
.CSV
FROM testabismdt
WHERE callid<1000 AND llID=ci
GROUP BY callid
HAVING AVG(MR_DL_RXQUAL)>rxqual_level;
SQL 2.5
END$$
DELIMITER ;

35

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2
-80dBm 4 /

DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `interfere_up`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `interfere_up`()
BEGIN
DECLARE a VARCHAR(255);
DECLARE b VARCHAR(255);
DECLARE c VARCHAR(255);
a,b,c-80dbm 4

DECLARE 1name_cursor CURSOR FOR


SELECT COUNT(MR_UP_RXLEV)
FROM testabismdt
WHERE MR_UP_RXLEV>=31 AND MR_UP_RXLEV<63 AND mr_up_rxqual>=4;

DECLARE 2name_cursor CURSOR FOR


SELECT COUNT(MR_valid)
FROM testabismdt
WHERE MR_valid>=0;

OPEN 1name_cursor;
OPEN 2name_cursor;
FETCH 1name_cursor INTO a;
FETCH 2name_cursor INTO b;
SET c= a/b
SELECT c AS rate;

CLOSE 1name_cursor;
CLOSE 2name_cursor;
END$$
DELIMITER ;

36

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

3
6 7 / 20%
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS ``$$
CREATE DEFINER=`root`@`localhost` PROCEDURE ``()
BEGIN
DECLARE a VARCHAR(255);
DECLARE b VARCHAR(255);
DECLARE c VARCHAR(255);
a,b,c 6 7
Select count(MR_DL_RXQUAL) into a
From testabismdt
Where MR_DL_RXQUAL=6 OR MR_DL_RXQUAL=7;
A
DECLARE 2name_cursor CURSOR FOR
SELECT COUNT(MR_valid)
FROM testabismdt
WHERE MR_valid>=0;

OPEN 2name_cursor;
FETCH 2name_cursor INTO b;

SET c= a/b
SELECT c AS rate;

CLOSE 2name_cursor;
END$$
DELIMITER ;

4
-90dBm TA 0 1 *100%/
30%
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS ``$$
CREATE DEFINER=`root`@`localhost` PROCEDURE ``()
37

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

BEGIN
DECLARE a VARCHAR(255);
DECLARE b VARCHAR(255);
DECLARE c VARCHAR(255);
a,b,c-90dBm TA 0 1

SELECT COUNT(MR_DL_RXLEV) into a


FROM testabismdt
WHERE MR_DL_RXLEV<=20 AND(MR_TA=0 OR MR_TA=1);
-90dBm TA 0 1
SELECT COUNT(MR_valid) into b
FROM testabismdt
WHERE MR_valid>=0;

SET c= a/b;
SELECT c AS rate;

END$$
DELIMITER ;

5
GSM LAC
LAC , MSC PAGING MS IMSI TMSI

A
=/100%;

callid
1

L
A
C
5
4
3
4
5

I
M
S
I
4
6
0
9
3
8
4915

TMSI

MESSAGETYPE
Paging

5
4
3
4
5

14013232

Paging

2
2

5
4
3
4
2
5
4
3
4
5

5
4
3
4
2

34543345

Paging

3
4

5
4
3
4
4
5
4
3
4
2

34543345

Paging
Paging

5
4
3
4
1

14013232

Paging

5
4
3
4
2

4
6
0
9
3
8
4938
4
6
0
9
3
8
4938

Paging
Paging

4
6
0
9
3
8
4934
4
6
0
9
3
8
4934

Paging

mm
CALLid IMSI TMSI MS
MS IMSI TMSI
CALLID IMSI TMSI
sql countpaging response/count(paging)
38

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

.
4.1
MySQL

MySQL
1000

mm

c
a
l
l
i
d
1
1
2
2
3
3
4
4
4

l
l
i
d
1
4
5
3
5
4
3
4
5
1
4
5
3
5
4
3
4
5
1
4
5
3
5
4
3
4
5
1
4
5
3
5
4
3
4
5
1
4
5
3
5
4
3
4
6
1
4
5
3
5
4
3
4
6
1
4
5
3
5
4
3
4
6
1
4
5
3
5
4
3
4
5
1
4
5
3
5
4
3
4
5

IMSI
4609384915

TMSI
14013232

4609384938
4609384938
34543345
34543345
4609384934
14013232
4609384934

MESSAGETYPE
Paging
Paging
Paging
Paging
Paging
Paging
Paging
Paging
Paging

CALID =4 SQL
SELECT called from mm where called=4; SQL
SQL callid =1 callid=4

SQL
CALLID SELECT called from mm where called=4;
SQL CALLid=4

4.2

1 Create Index indexName ON tableName(tableColumns(length));


CHAR,VARCHAR length ; BLOB TEXT
length
2Alter tablename Add Index [indexName] on (tableColumns(length))
3 Create table tablename ( [...], INDEX [indexName]

39

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

(tableColumns(length))

""

1CREATE UNIQUE INDEX indexName ON tableName(tableColumns(length))


2
ALTER tableName ADD UNIQUE [indexName] ON (tableColumns(length))
3CREATE TABLE tableName ( [...], UNIQUE [indexName]
(tableColumns(length));

CREATE TABLE testIndex(i_testID INT NOT NULL AUTO_INCREMENT,vc_Name VARCHAR(16)


NOT NULL,PRIMARY KEY(i_testID)); ALTER

(""

Leftmost Prefixing
firstnamelastnameage fname_lname_age
MySQL fname_lname_age
firstnamelastnameage
firstnamelastname
firstname

4.3
WHERE JOIN MySQL
<<==>>=BETWEENIN LIKE()
EXPLAIN

4.4 EXPLAIN
EXPLAIN
SELECT
40

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

explain mysql select

EXPLAIN changchunabismdt select

Explain
Id
table changchunabismdt
type consteq_reg
refrangeindexhe ALL
possible_keys
WHERE possible_keys speed

key NULLspeed;
key_len. 9
rowsMYSQL

Extra
MYSQL using where ;using index;
extra
Distinct: mysql
Not exists: MYSQL LEFT JOIN LEFT JOIN

Using filesort: MYSQL

Using index:

Where used WHERE

4.5
MDT CALLid=0
callid SELECT
changchunabismdt callid speed
41

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

Speed

sql

rows 36788 sql 36788


explain

133865

42

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

.
5.1

over over

begin
transaction end transaction

5.2
4

1SELECT LOCK IN SHARE MODE


commit

SELECT
2SELECT FOR UPDATE
SELECT * FROM tablename WHERE id<200
43

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

id<200 SELECT LOCK IN


SHARE MODE

SELECT LOCK IN SHARE


MODE
3INSERT / UPDATE / DELETE

5.3

insert
updatedelete

5.4
1
123

create table book


(
book_id unsigned int(10) not null auto_increment,
book_name varchar(100) not null,
book_price float(5, 2) not null, # 999.99
book_number int(10) not null,
primary key (book_id)

1. SELECT book_number FROM book WHERE book_id = 123;


book_number book_number
2. UPDATE book SET book_number = book_number - 1 WHERE book_id = 123;

1. SELECT book_number FROM book WHERE book_id = 123;


book_number

44

Generated by Foxit PDF Creator Foxit Software


http://www.foxitsoftware.com For evaluation only.

2. UPDATE book SET book_number = book_number - 1 WHERE book_id = 123;

book_number -1
unsigned

sql

START TRANSACTION BEGIN


COMMIT
ROLLBACK
SET AUTOCOMMIT = {0 | 1} autocommit

BEGIN;
SELECT book_number FROM book WHERE book_id = 123 FOR UPDATE;
UPDATE book SET book_number = book_number - 1 WHERE book_id = 123;
COMMIT;
------------------------------------------------------------------------------ FOR UPDATE
SELECT ... FOR UPDATE

FOR UPDATE SELECT

45

You might also like