By Sikha Saha Bagui, Richard Walsh Earp
Book Price: $44.99 USD
£31.99 GBP
PDF Price: $35.99
Cover | Table of Contents
http://www.cs.uwf.edu/~sbagui. The load script will create the Student_course database for you. This database will be used throughout the rest of the book to learn SQL. At this point, you may want to copy the load script, SQLServer2005_load.sql, to your working directory on your computer, before you start working on the next section. Right-click on the script on the web site, select Save Target As, and save it to your working directory.enter the appropriate server type and server name, and select Windows Authentication. Then, click Connect.
enter the appropriate server type and server name, and select Windows Authentication. Then, click Connect.
Student_course.
Student_course. You may leave the Owner as <default
> for now, as shown in Figure 1-5. Click OK. You will get the screen shown in Figure 1-6.
Student_course database has now been created. Note the newly created Student_course database icon under the Summary tab on the righthand side of the screen below Databases (see Figure 1-6).Student_course database under the Object Explorer (on the left side of your screen) right away, you may have to first right-click on the Databases node and then select Refresh.Student_course database node under and Databases (under the Object Explorer on the left portion of your screen), as shown in Figure 1-7.Student_course database node by clicking on the + sign beside the Student_course node, and you will get the screen shown in Figure 1-8, which shows the default objects that are in the Student_course database and right-click, as shown in Figure 1-11. Select New Query.SQLQuery1.sql by default. Later we will show you how to change the name of the query when saving it.Student_course database automatically becomes the database against which the queries are executed, because you initially selected Student_course and then right-clicked. If we want to work in our Student_course database, we have to make sure that the Student_course database is active. If the Student_course database is not active, we have to activate it—we show you how to do this in different ways in the following sections.
Student_course database), you will get Figure 1-14. Here, note that the Student_course database is opened or activated, you need to create tables in the Student_course database and insert data into the tables. To do this, run (execute) the load script, SQLServer2005_load.sql, that you downloaded and saved to your working directory.Student_course database is active. Paste the load script into the query editor, as shown in the Figure 1-17.
Execute button or the F5 shortcut key. This script takes only a few seconds to execute. You will get the results shown in Figure 1-18--on the bottom part of the screen under the Messages tab.
Cap, Course, Department_to_major, Dependent, Grade_report, Plants, Prereq, Room, Section, Student, and teststu, in the Student_course database and inserts data into them. The tables in the Student_course database are laid out in Appendix A. We also present the T-SQL for the load script in Appendix B.Student_course node and then expand the Tables node. You will get the screen shown in Figure 1-19. Every table shows up as a node under Student_course.Student table for example, expand the Student node by clicking on the + sign beside it, and then expand the Columns node, by clicking on the + sign beside it, as shown in Figure 1-20. You will be able to view the columns
in the Student table. The columns in the Student table are stno, sname, major, class, and bdate.
SNAME field of the Student table, as seen in Figure 1-20, right-click the SNAME field of the Student table (as shown in Figure 1-21), and select one of the following options—New Column, Modify, Rename, Delete, Refresh or Properties.
Student--and then select Modify, as shown in Figure 1-22.Student table is now displayed, as shown in Figure 1-23.
Student table, right-click on the Student table, and select Open Table. This will show all 48 rows of the Student table, of which we show the first 14 rows here:
STNO SNAME MAJOR CLASS BDATE
----- ------- ------ ----- ----------------------
2 Lineas ENGL 1 4/15/1980 12:00:00 AM
3 Mary COSC 4 7/16/1978 12:00:00 AM
8 Brenda COSC 2 8/13/1977 12:00:00 AM
10 Richard ENGL 1 5/13/1980 12:00:00 AM
13 Kelly MATH 4 8/12/1980 12:00:00 AM
14 Lujack COSC 1 2/12/1977 12:00:00 AM
15 Reva MATH 2 6/10/1980 12:00:00 AM
17 Elainie COSC 1 8/12/1976 12:00:00 AM
19 Harley POLY 2 4/16/1981 12:00:00 AM
20 Donald ACCT 4 10/15/1977 12:00:00 AM
24 Chris ACCT 4 2/12/1978 12:00:00 AM
34 Lynette POLY 1 7/16/1981 12:00:00 AM
49 Susan ENGL 3 3/11/1980 12:00:00 AM
62 Monica MATH 3 10/14/1980 12:00:00 AM
.
.
.
Student_course database should be active or open.Student_course and then select New Query. Type the following SQL query in the resulting screen:
USE Student
SELECT *
FROM Student
USE Student opens the Student_course database, as shown in Figure 1-12. SELECT is the SQL keyword that means "select data" or "retrieve the following data from the database." The * is interpreted to mean "show all columns in the result." FROM is the keyword that names the source of the data, and Student is the name of a table. So this is a simple SQL query that tells SQL Server to display all the rows and columns (all the data) in the Student table.Execute button, shown in Figure 1-25. If there are no errors in the query, the Execute button will execute (run) the query and the results will show on the results pane (bottom partition) of the screen.
Student_course database that we will be using throughout the rest of this book. In addition, we have demonstrated how to work with tables. We have shown you how to type, parse, execute and save a simple query. In the process, we have also familiarized you with the main screens and workings of SQL Server 2005's Management Studio. Towards the end of the chapter, we showed you how to change (or customize) some of SQL Server 2005's default settings to suit your needs.model database?master database?tempdb database?USE command?Student_course database are shown in Appendix A.Student_course database contains the following tables: Student, Dependent, Grade_report, Section, Department, Course, Prereq, Room, Cap, Plants.Student table. (Hint: To retrieve all columns, use SELECT * in your query; the * means "all columns"). Save and execute the query. Save the results to a file and print out the results.SELECT statement. We examine how to retrieve data from a table by the use of SELECT statements, how to SELECT fields (columns) and rows from tables, how to use the ORDER BY and WHERE clauses, and how to use the AND, OR, and BETWEEN operators. The concept of COUNT and null values is also to be established. Then, to make writing queries simpler, we discuss how to use table and column aliases, table qualifiers, synonyms, and finally we present a convention for writing SQL statements.SELECT command on the table. SELECT is usually the first word in a SQL statement or query. The SELECT statement returns information from a table (or a set of tables, the database) as a set of records, or a result set. The result set is a tabular arrangement of data, composed of rows and columns. The SELECT statement shows the output on the computer screen (as shown in Figures 1-26 and 1-28 of Chapter 1). It does not save the results. The simplest and most commonly used form of the SELECT syntax is:
SELECT fields (a.k.a. columns or attributes)
FROM Table
Table is the name of the table from which the data will be displayed, and fields are the columns (attributes) that you chose to display from the named table. If you did not know the name of the columns in the table, or you wanted to display all the columns in the table, you would use an asterisk (*) in place of fields; substituting an asterisk (*) in place of fields would list all the columns in the table.SELECT statement gives us a result set that is composed of the data from columns of a table.SELECT command on the table. SELECT is usually the first word in a SQL statement or query. The SELECT statement returns information from a table (or a set of tables, the database) as a set of records, or a result set. The result set is a tabular arrangement of data, composed of rows and columns. The SELECT statement shows the output on the computer screen (as shown in Figures 1-26 and 1-28 of Chapter 1). It does not save the results. The simplest and most commonly used form of the SELECT syntax is:
SELECT fields (a.k.a. columns or attributes)
FROM Table
Table is the name of the table from which the data will be displayed, and fields are the columns (attributes) that you chose to display from the named table. If you did not know the name of the columns in the table, or you wanted to display all the columns in the table, you would use an asterisk (*) in place of fields; substituting an asterisk (*) in place of fields would list all the columns in the table.SELECT statement gives us a result set that is composed of the data from columns of a table.SELECT statement, we have to make sure that the right database is open. To open a database that you want to use, type the following in the query editor screen (the query editor screen is shown in Figure 1-12 of Chapter 1):USE Student_course
Student_course is the name of the database that we would like to open. The Student_course database should now be active.Student_course database is active, to display all the data from a table called Dependent from our database (Dependent(pno, dname, relationship, sex, age)
WHERE the dependents are older than five, or, only the rows WHERE the dependents are female. That is, you want to display only the rows that meet a certain condition or criteria.WHERE clause in a SELECT statement, you can selectively choose rows that you wish to display based on a criterion. For additional filtering, the WHERE clause can be used with logical operators like AND and OR, and the BETWEEN operator and its negation, NOT BETWEEN.WHERE clause is a row filter that is used to restrict the output of rows (or tuples
) in a result set. When the WHERE clause is used, the SQL Server database engine selects the rows from the table for the result set that meet the conditions listed in the WHERE clause. So, as we have previously illustrated, if no WHERE clause is used in a query, the query will return all rows from the table.SELECT statement with a WHERE clause:COUNT function is used to return a count of the number of rows that the output will produce, without actually displaying all of the output (rows) themselves. This function often comes in handy when you have large tables, or you expect a large output. In such situations, it is desirable to determine the number of rows of output that you will be getting before actually displaying the output. In this section, we introduce the COUNT function and we also take another look at the concept of null values.SELECT * FROM Dependent
Dependent table plus all the values for all columns in those rows. If you want to know only the number of rows in the output (rather than view the actual rows themselves), type the following:SELECT COUNT(*) FROM Dependent
----------- 39 (1 row(s) affected)
Dependent table. Note that the actual rows themselves are not displayed.SELECT COUNT(age) FROM Dependent
----------- 36 Warning: Null value is eliminated by an aggregate or other SET operation. (1 row(s) affected)
COUNT(age) counts only the rows in which age is not null, meaning that it counts only the rows that have a defined value. Therefore, the preceding output is 36 rows rather than 39 rows because the age column in the Dependent table includes 3 null values. If you want COUNT to count rows and include rows that have fields with null values, you would use COUNT(*). In the next section, we discuss null values in more detail.IS NULL condition is the only condition that directly tests for nulls. Null values are unmatched by all other conditions in SELECT statement with the use of a WHERE clause and logical operators. In this section, we introduce the ROWCOUNT function, another way of limiting the number of rows that can be the returned by a SELECT statement.WHERE clause assumes that you have knowledge of the actual data values present in a data set. But what if you want to see only a sample of a result set, and you have no idea which range of values are present in the table? In this case, the ROWCOUNT function can come in handy.Dependent table, you can type:SET ROWCOUNT 10 SELECT * FROM Dependent
PNO DNAME RELATIONSHIP SEX AGE ------ -------------------- ------------ ---- ------ 2 Matt Son M 8 2 Mary Daughter F 9 2 Beena Spouse F 31 10 Amit Son M 3 10 Shantu Daughter F 5 14 Raju Son M 1 14 Rani F 3 17 Susan Daughter F 4 17 Sam Son M 1 20 Donald II Son M NULL (10 row(s) affected)
ROWCOUNT, you should reset the ROWCOUNT property by:SET ROWCOUNT 0
ROWCOUNT property, you will keep getting whatever you set your ROWCOUNT to for the remainder of this session (that is, until you log off).ROWCOUNT and issue multiple queries in the same batch, the rows are limited for all queries within the batch.SELECT statement.SELECT dname, age, sex FROM Dependent WHERE age > 5
dname age sex -------------------- ------ ---- Matt 8 M Mary 9 F Beena 31 F Chris 6 M Tom 45 M James 14 M Hillary 16 F Phoebe 12 F Om 6 M Barbara 26 F . . . (17 row(s) affected)
Dependent table for the column headings. These column names may not be so explicit or descriptive. For example, what is dname? We would probably assume it's a name of something, but what does the "d" in front of name stand for? Using more descriptive headings in the output would considerably increase readability. To use more descriptive column headings, you can include column aliases just before or after the column name by using AS in the SELECT statement, as shown next (in the first few examples, we place the descriptive column headings after the column names):SELECT dname AS Dependent_name, age AS Dependent_age, sex AS Dependent_sex FROM Dependent WHERE age > 5
Dependent_name Dependent_age Dependent_sex -------------------- ------------- ------------- Matt 8 M Mary 9 F Beena 31 F Chris 6 M Tom 45 M James 14 M Hillary 16 F Phoebe 12 F Om 6 M Barbara 26 F . . . (17 row(s) affected)