dessoft - queries, how to do it

26
© 2010 DesSoft Queries Version: 1.0.0

Upload: dessoft-control-and-instrumentation-engineering-software

Post on 26-Jun-2015

64 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: DesSoft - Queries, how to do it

© 2010 DesSoft

Queries

Version: 1.0.0

Page 2: DesSoft - Queries, how to do it

All rights reserved. No parts of this work may be reproduced in any form or by any means - graphic, electronic, ormechanical, including photocopying, recording, taping, or information storage and retrieval systems - without thewritten permission of the publisher.

Products that are referred to in this document may be either trademarks and/or registered trademarks of therespective owners. The publisher and the author make no claim to these trademarks.

While every precaution has been taken in the preparation of this document, the publisher and the author assume noresponsibility for errors or omissions, or for damages resulting from the use of information contained in this documentor from the use of programs and source code that may accompany it. In no event shall the publisher and the author beliable for any loss of profit or any other commercial damage caused or alleged to have been caused directly orindirectly by this document.

Printed: February 2010 in Eldoraigne, South Africa

Queries

© 2010 DesSoft

Publisher Special thanks to:

All the people who contributed to this document.DesSoft

Page 3: DesSoft - Queries, how to do it

3Contents

© 2010 DesSoft

Table of Contents

Foreword 0

Section I The Query Builder 6

Section II The SELECT Statement 10

................................................................................................................................... 111 Exercise

Section III The WHERE Clause 14

................................................................................................................................... 161 Exercise

Section IV The UPDATE Statement 20

................................................................................................................................... 211 Exercise

Section V The JOIN Keyword 24

................................................................................................................................... 261 Exercise

Index 0

Page 4: DesSoft - Queries, how to do it
Page 5: DesSoft - Queries, how to do it

Queries

DesSoft - Engineering Design Documentation Software

Section

I

Page 6: DesSoft - Queries, how to do it

6 Queries

© 2010 DesSoft

1 The Query Builder

The Query Builder has 7 steps to complete in order to build your query.

1. Specify the type of query and a name for the query.2. Select the tables required for the query.3. Specify the fields in the selected tables that will be used to join them together.4. Select the fields that must be displayed in the query.

Also specify field aliases and filters (WHERE statement) here.

5. Specify the order in which these fields must be displayed in, in the query.6. Displays the built query for your review.7. Displays the result of the built query.

Page 7: DesSoft - Queries, how to do it

7The Query Builder

© 2010 DesSoft

Page 8: DesSoft - Queries, how to do it
Page 9: DesSoft - Queries, how to do it

Queries

DesSoft - Engineering Design Documentation Software

Section

II

Page 10: DesSoft - Queries, how to do it

10 Queries

© 2010 DesSoft

2 The SELECT Statement

What is the SELECT Statement?

The SELECT statement is used to select/draw data from a database.

SELECT Statement Syntax

SELECT column_name(s)FROM table_name

and

SELECT * FROM table_name

Example 1

The "Panel" table:

TagNum Location PType PanelID 10-JB-01 Field FLD 122-PLC-02 Control Room CRD 5MCC1/T01-F01 MCC1 MCC 9

Now we want to select the content of the "TagNum" and "Location" columns from the "Panel" table above.

We use the following SELECT statement:

SELECT TagNum, Location FROM Panel

The result will look like this:

TagNum Location 10-JB-01 Field22-PLC-02 Control RoomMCC1/T01-F01 MCC1

Example 2

Now we want to select ALL the columns from the "Panel" table.

We use the following SELECT statement:

SELECT * FROM Panel

The asterisk (*) is a quick way of selecting all columns!

The result will look like this:

TagNum Location PType PanelID 10-JB-01 Field FLD 122-PLC-02 Control Room CRD 5MCC1/T01-F01 MCC1 MCC 9

Page 11: DesSoft - Queries, how to do it

11The SELECT Statement

© 2010 DesSoft

2.1 Exercise

1. Write a select query to find all the instruments in the IIndex table.2. Display all the panels and the area they are located in.3. Assemble a list of the project's card standards.4. Display a list of all the cables in the project, as well as their source and destination.5. Display all the information you can find on the project's existing electrical equipment.6. Make a list of all the I/O cards and what type of cards they are.

Page 12: DesSoft - Queries, how to do it

12 Queries

© 2010 DesSoft

Page 13: DesSoft - Queries, how to do it

Queries

DesSoft - Engineering Design Documentation Software

Section

III

Page 14: DesSoft - Queries, how to do it

14 Queries

© 2010 DesSoft

3 The WHERE Clause

What is the WHERE Clause?

The WHERE clause is used to filter records.

WHERE Clause Syntax

SELECT column_name(s)FROM table_nameWHERE column_name operator value

Example

The "Panel" table:

TagNum Location PType PanelID 10-JB-01 Field FLD 110-JB-02 Field FLD 2110-JB-03 Field FLD 6610-JB-04 Field FLD 1122-PLC-02 Control Room CRD 5MCC1/T01-F01 MCC1 MCC 9

Now we want to select only the field panels and ALL their data from the table above.

We use the following SELECT statement:

SELECT * FROM PanelWHERE PType='FLD'

The result will look like this:

TagNum Location PType PanelID 10-JB-01 Field FLD 110-JB-02 Field FLD 2110-JB-03 Field FLD 6610-JB-04 Field FLD 11

Quotes Around Text Fields

SQL uses single quotes around text values (most database systems will also accept doublequotes).Although, numeric values should not be enclosed in quotes.

For text values:

This is correct:

SELECT * FROM Panel WHERE PType='FLD'

This is wrong:

SELECT * FROM Panel WHERE PType=FLD

Page 15: DesSoft - Queries, how to do it

15The WHERE Clause

© 2010 DesSoft

For numeric values:

This is correct:

SELECT * FROM Panel WHERE Area=22

This is wrong:

SELECT * FROM Panel WHERE Area='22'

Operators Allowed in the WHERE Clause

With the WHERE clause, the following operators can be used:

Operator Description

= Equal<> Not equal> Greater than< Less than

>= Greater than or equal <= Less than or equal

BETWEEN Between an inclusive rangeLIKE Search for a pattern

IN If you know the exact value you want to return for at least one of the columns

In some versions of SQL the <> operator may be written as !=

Page 16: DesSoft - Queries, how to do it

16 Queries

© 2010 DesSoft

3.1 Exercise

1. Display all the electrical cables that have a cable standard associated with them.2. Display all the instrumentation documents.3. Display all the fieldbus cables that contain "2000" in the cable number.4. Display all the motors with an absorb power greater than 1.55. Display all the motor standards that have a rating of 200 and less.6. Display all the control instruments that don't belong to a loop.

Page 17: DesSoft - Queries, how to do it

17The WHERE Clause

© 2010 DesSoft

Page 18: DesSoft - Queries, how to do it
Page 19: DesSoft - Queries, how to do it

Queries

DesSoft - Engineering Design Documentation Software

Section

IV

Page 20: DesSoft - Queries, how to do it

20 Queries

© 2010 DesSoft

4 The UPDATE Statement

What is the UPDATE Statement?

The UPDATE statement is used to update existing records in a table.

UPDATE Statement Syntax

UPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies/filters whichrecord(s) should be updated. If you omit the WHERE clause, ALL records in the table will beupdated!

Example

The "Panel" table:

TagNum Location PType PanelID 10-JB-01 Field FLD 122-PLC-02 Control Room CRD 5MCC1/T01-F01 MCC1 MCC 9

Now we want to update the "Location" column of "22-PLC-02" to "PLC Room" in the "Panel"table above.

We use the following SQL statement:

UPDATE PanelSET Location="PLC Room"WHERE TagNum="22-PLC-02"

The "Panel" table will now look like this:

TagNum Location PType PanelID 10-JB-01 Field FLD 122-PLC-02 PLC Room CRD 5MCC1/T01-F01 MCC1 MCC 9

If we had omitted the WHERE clause in the example above, like this:

UPDATE PanelSET Location="PLC Room"

The "Panel" table would have looked like this:

TagNum Location PType PanelID 10-JB-01 PLC Room FLD 122-PLC-02 PLC Room CRD 5MCC1/T01-F01 PLC Room MCC 9

ALWAYS test update queries in a dummy database first OR make a backupof your database before executing the query!!

Page 21: DesSoft - Queries, how to do it

21The UPDATE Statement

© 2010 DesSoft

4.1 Exercise

1. Rename all the cable numbers to the "source/destination" format2. Update all the signal instruments' functional description to "Control Signal"3. Change the manufacturer of all the instrument standards that contain "con" in the model to

"Training"4. All control cables' derating factor must be void/nullified.5. All the loop connection diagrams in area 22's drawing number must contain the diagram's

description as well. The drawing number and the description must be separated by a dash.

Page 22: DesSoft - Queries, how to do it

22 Queries

© 2010 DesSoft

Page 23: DesSoft - Queries, how to do it

Queries

DesSoft - Engineering Design Documentation Software

Section

V

Page 24: DesSoft - Queries, how to do it

24 Queries

© 2010 DesSoft

5 The JOIN Keyword

What is the JOIN Keyword?

The JOIN keyword is used in a query to return data from two or more tables, based on arelationship between certain columns in these tables.

Tables in a database are often related to each other with keys.

A primary key is a column (or a combination of columns) with a unique value for each row.Each primary key value must be unique within the table. The purpose is to bind data together,across tables, without repeating all of the data in every table.

Look at the "Loop" table:

LoopID TagNum Service 1 10-F-01 Demin water flow2 22-L-05 Level TK-0023 22-T-03 Temperature

Note that the "LoopID" column is the primary key in the "Loop" table. This means that no tworows can have the same LoopID.

Next, we have the "IIndex" table:

IIndexID TagNum LoopID 10 10-FT-01 114 22-LT-05 223 22-LY-05 228 22-TE-03 329 22-TT-03 3

Note that the "IIndexID" column is the primary key in the "IIndex" table and that the "LoopID"column refers to the loops in the "Loop" table without using their tag numbers.

Notice that the relationship between the two tables above is the "LoopID" column.

Example

We want to list all the Instruments as well as the loops they belong to.

We use the following SQL statement:

SELECT IIndex.TagNum, Loop.TagNumFROM (IIndex INNER JOIN Loop ON IIndex.LoopID=Loop.LoopID)

The result will look like this:

IIndex.TagNum Loop.TagNum 10-FT-01 10-F-0122-LT-05 22-L-0522-LY-05 22-L-0522-TE-03 22-T-0322-TT-03 22-T-03

Page 25: DesSoft - Queries, how to do it

25The JOIN Keyword

© 2010 DesSoft

Different types of JOIN statements

JOIN Return rows when there is at least one match in both tables

INNER JOIN Is the same as JOIN

LEFT JOIN Return all rows from the left table, even if there are no matches in the right table

RIGHT JOIN Return all rows from the right table, even if there are no matches in the left table

FULL JOIN Return rows when there is a match in one of the tables

Page 26: DesSoft - Queries, how to do it

26 Queries

© 2010 DesSoft

5.1 Exercise

1. Write a query that will display all the loop numbers with their loop drawing numbers.2. Write a query that will display all the field panels and their tstrips.3. Display all the cables that belong to panels.4. Display all the devices that are associated to a device standard that doesn't have a vendor

specified.5. Display all the cables that are not aberdare cables.