SQL Data Manipulation Basics (primary) 3

Source: Internet
Author: User
Data manipulation fields

Typically, when you remove a field value from a table, the value is associated with the field name that was defined when the table was created. If you select all the author names from the table authors, all values will be associated with the field name au_lname. But in some cases, you need to manipulate the field names. In the SELECT statement, you can replace it with a new name only after the default field name. For example, you can replace the field name au_lname with a more intuitive and readable name author last name:

Select au_lname ' Author last Name ' from authors

When this SELECT statement executes, the value from the field au_lname is associated with "Author last Name". This may be the result of the query:

Author last Name

................................................................................

White

Green

Carson

O ' Leary

Straight

...

(Affected row (s))

Note that the field header is no longer au_lname, but is replaced by author last name.

You can also manipulate the value of a field returned from a table by performing an operation. For example, if you want to double the price of all the books in the table titles, you can use the following
SELECT statement:

SELECT Price*2 from titles

When this query is executed, the price of each book is doubled when it is taken out of the table. However, manipulating a field in this way does not change the price of the book stored in the table. The transport of a field
The calculation affects only the output of the SELECT statement, not the data in the table. To display the original price of the book and the new price after the increase, you can use the following query:

Select Price ' Original price ', price*2 ' New price ' from titles

When the data is taken out of the table titles, the original price is displayed under the title original price, and the doubled prices are displayed under the title new value. The result could be this
Sample:

Original Price New Price

.........................................................................

39.98
11.95 23.90

5.98
39.98
...

(s) affected)

You can use most of the standard mathematical operators to manipulate field values, such as add (+), subtract (-), multiply (*), and divide (/). You can also ship multiple fields at a time
To calculate, for example:

Select Price*ytd_sales "Total revenue" from titles

In this example, the total sales of each type of book are calculated by multiplying the price with the sales volume. The result of this SELECT statement would be this:

Total revenue

.....................................................

81,859,05

46,318,20

55,978,78

81,859,05

40,619,68

...

(s) affected)

Finally, you can use the Join operator (which looks like a plus sign) to connect two-character fields:

SELECT au_fname+ "" +au_lname "author name" from authors

In this example, you paste the field au_fname and the field au_lname together, separated by a comma, and specify the query result title as author
Name The execution result of this statement will be this:

Author names

..................................................................

Johnson White

Marjorie Green

Cheryl Carson

Michael O ' Leary

Dean Straight

...

(Affected row (s))

As you can see, SQL provides you with a lot of control over query results. You should make full use of these advantages in the ASP programming process. Using SQL to manipulate query results is almost always
More efficient than using scripts that have the same effect.

Sort Query Results

The introduction of this chapter has emphasized that there is no intrinsic order for SQL tables. For example, it does not make sense to take a second record from a table. From the perspective of SQL, there is no record
Prior to any other record.

However, you can manipulate the order of the results of an SQL query. By default, records do not appear in a particular order when records are taken out of a table. For example, when from a table
When you remove the field au_lname from the authors, the query results appear as follows:

au_lname

...........................................

White

Green

Carson

O ' Leary

Straight

...

(Affected row (s))

It is inconvenient to look at a list of names that are not in a particular order. If you put these names in alphabetical order, it would be much easier to read them. By using the ORDER BY clause, you can
To force a query result to be sorted in ascending order, like this:

SELECT au_lname from authors order by au_lname

When this SELECT statement is executed, the display of the author's name is sorted alphabetically. The ORDER BY clause arranges the author's name in ascending sequence.

You can also use the ORDER BY clause for multiple columns at the same time. For example, if you want to display both field au_lname and field au_fname in ascending order, you need to have two fields
To sort:

SELECT Au_lname,au_fname from authors to au_lname, au_fname

The query first sorts the results by au_lname fields, and then sorts them by field au_fname. The records are taken out in the following order:

au_lname au_fname

...............................................................................

Bennet Abraham

Ringer Albert

Ringer Anne

Smith Meander

...

(Affected row (s))

Note that there are two authors with the same name ringer. The author, named Albert Ringer, appeared before the author named Anne Ringer, because Albert was alphabetically
Preface should precede the surname Anne.

If you want to put the query results in reverse order, you can use the keyword desc. Keyword desc the query results in descending order, as shown in the following example:

SELECT Au_lname,au_fname from Authors

WHERE au_lname= "Ringer" ORDER by au_lname, au_fname DESC

This query extracts all author records named Ringer from the table authors. The ORDER BY clause sorts the results of the query according to the author's first and last name. The result is
such as:

au_lname au_fname

....................................................................................................

Ringer Anne

Ringer Albert

(2 row (s) affectec)

Notice that in this table, the surname Anne appears before the surname Albert. The author's name appears in descending order.

You can also sort a query result by a numeric field. For example, if you want to remove the price of all books in descending order, you can use the following SQL query:

SELECT price from titles to price DESC

This SELECT statement takes out the price of all the books from the table, showing the results, the low price of the book first shows that the high price of the book after the display.

Warning:

Do not sort the query results when they are not specifically needed, because the server takes some effort to complete the work. This means that a SELECT statement with an ORDER BY clause holds
It takes a long time to line up than a typical SELECT statement.

Take out a different record

It is possible for a table to have duplicate values in the same column. For example, the names of two authors in the table authors of the database pubs are ringer. If you take everything from this table,
Name, the name Ringer will be shown two times.

In certain situations, you may only be interested in taking a different value out of a table. If a field has duplicate values, you may want each value to be selected only once, and you can
To use the keyword distinct to do this:

Selcet DISTINCT au_lname from authors WHERE au_lname= "Ringer"

When this SELECT statement executes, only one record is returned. You can delete all duplicate values by including the keyword distinct in the SELECT statement. For example, suppose
There is a table about newsgroup information release, you want to take out all the names of people who have posted information in this newsgroup, then you can use the keyword distinct. each with
The name of the household is taken only once--although some users have published more than one piece of information.

Warning:

As with the ORDER BY clause, forcing the server to return distinct values increases the running cost. The blessing had to take some time to finish the work. Therefore, it is not
Do not use the keyword distinct when necessary.

Create a new table

As I said earlier, all the data in the database is stored in the table. Data tables include rows and columns. The column determines the type of data in the table. The row contains the actual data.

For example, the table authors in pubs in the database has nine fields. One of these fields is named au_lname, which is used to store the author's name information. Each time a new author is added to the table, the author's name is added to the field, creating a new record.

By defining a field, you can create a new table. Each field has a name and a specific data type (the data type is described in the following "field type" section), for example, the field au_lname stores character data. A field can also store other types of data.

There are many ways to create a new table using SQL Sever. You can execute an SQL statement or use the SQL Transaction manager (SQL Enterprise Manager) to create a new table. In the next section, you'll learn how to create a new table with SQL statements.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.