Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Introduction to Programming

Structured Query Language

Primary Key of a Table


If a table contains one or more columns that uniquely identify each row in the table, you can define these columns as the primary key of the table. For instance, the primary key of the Student table is the StudentID column.

Introducing the Structured Query Language


Structured Query Language (SQL) is a specialized set of database programming commands that enables the developer or other database users to perform tasks in a database management system. Some examples of the types of tasks you can perform with SQL are: Retrieve data from one or more tables in one or more databases.
3

Introducing the Structured Query Language


Change data in tables by inserting, deleting, and updating records. Obtain summary information about the data in tables such as total and average values. Create, modify, or delete database objects such as tables, indexes, and queries.
4

Introducing the Structured Query Language


A SQL statement contains one or more lines of text, much like a code statement in Visual Basic. SQL statements fall into two basic categories: The first three items in the list are examples of how SQL is used to manipulate data in the database, the last item demonstrates how SQL can be used to define the database itself.
5

Introducing the Structured Query Language

Introducing the Structured Query Language


Note Data-manipulation language (DML), and data-definition language (DDL) are terms used to categorize SQL statements.

Using the SELECT Statement


The SELECT statement is the most frequently used SQL command. It retrieves records (or specified fields from records) from one or more tables in the database. The SELECT statement, take the following general form: SELECT fieldlist FROM tablename [WHERE where clause]

Using the SELECT Statement


The simplest, most common example of a SELECT statement is selecting all fields from a single table. To display all of the data in the person table, execute the following SELECT statement: SELECT * FROM Person The statement would list every field of every record in the Person table.
9

Using the SELECT Statement


Person Table

10

Using the SELECT Statement


Note
SQL keywords such as SELECT are not case sensitive. However, table and field names may be case sensitive depending on the configuration of your database management system.

11

Choosing the Field List


In the field list portion of a SELECT statement, you can specify the asterisk (*) or a list of specific field names. The asterisk indicates that you want to display all fields in the table. If you want to display only certain fields, you need to include a comma-separated list of fields, as in the following example: SELECT SSN, LastName, FirstName FROM Person
12

Choosing the Field List

13

Choosing the Field List


In general, it is a good programming practice to specify a field list whenever possible, rather than just using asterisk, for the following reasons: Field lists return only the desired fields, minimizing network traffic. Field lists control the order in which fields are returned to the calling program. For example, you might decide to change the field order of the Person by adding a new field before the FirstName field, such as MiddleInitial. However, the order of fields returned by the SELECT statement would 14 be unaffected by these changes.

Filtering the Records


One of the most powerful features of SELECT (and other SQL commands) is the ability to control the records affected using a WHERE clause. A WHERE clause includes the keyword WHERE followed by a logical expression used to identify the desired records.

15

Filtering the Records


For example, to return only the names of the Silers in the Person table, you could execute the following SELECT statement: SELECT LastName, FirstName FROM Person WHERE LastName = Siler'

16

Filtering the Records


To return only the names of the Smiths in the Person table, you could execute the following SELECT statement: SELECT LastName, FirstName FROM Person WHERE LastName = 'Smith' This statement would return the LastName and FirstName fields only for those records that matched the WHERE expression; in this case, only records where the LastName field value is Smith.
17

Filtering the Records


Also note that the fields in the field list are independent from those in the WHERE clause. A WHERE clause can use any field from the tables specified in the FROM clause: SELECT LastName, FirstName FROM Person WHERE ZipCode='38117'

18

Filtering the Records


In this example, we are using the ZipCode field to filter records, but only displaying the name fields with ZipCode field.

19

Filtering the Records


To return the names, ages and addresses of the persons in the Person table who live in city Dallas, you could execute the following SELECT statement:
SELECT LastName, FirstName, Age, Address FROM Person WHERE City = Dallas
20

Filtering the Records

21

You might also like