您的位置:首页 > 数据库

Answer to Chapter 6 of O'Reilly Learning Sql on SQL Server 2005

2011-03-21 15:04 417 查看
6.6. Review Questions
1. Which has precedence, AND or OR?
AND

2. Why do we need derived structures?
Derived structures may become necessary as the queries we build get larger and we have to use a more step-by-step approach to find a result. Derived structures help us to build queries on top of other queries.

3. What is a view?
In SQL, a view (also called a virtual table) is a mechanism to procure a restricted subset of data that is accessible in ways akin to ordinary tables

4. List some advantages of using views.
It helps to develop a query step by step.
It can be used to restrict a set of users from seeing part of the database in a multiuser systemthis can be considered a security feature.
Views provide a layer of abstraction to data, facilitating backward compatibility and horizontal and vertical partitioning of data.
Views provide a seamless way to combine data from multiple sources.
Views do not occupy much disk space, as they have no data of their own.
When you use a view for queries, you use it just as you would use the underlying table(s).
Views can be used to create other views or queries.

5. List some advantages of using temporary tables.
 temporary tables are useful for doing work that requires multiple passes to avoid doing repetitive work. Temporary tables are useful for doing work on a "picture of the data" in the database.

6. Can temporary tables replace views in all cases?
NO. 

7. What is the difference between a view and temporary table?
Data in temporary tables is static and not reflective of updates to the original table(s).

8. What is the difference between a local temporary table and global temporary table?
Local temporary tables are created with # in front of the table name and are visible only to the user who is currently connected to the database. They are deleted when the user disconnects from this instance of SQL Server. They are local to the session in which they are created. Thus they are not visible in any other session, not even to one from the same host or login.

Global temporary tables are created with a prefix of ##. Global temporary tables can be accessed by anyone who logs onto the database, as long as the creator of the global temporary table is still logged on. The global temporary table will be dropped automatically when the session that created it ends and when all other processes that reference it have stopped referencing it. Therefore, even though the process that created the table may have ended, if another process is still using it, then it will still be alive.

9. If data is changed in a view, is it changed in the original table?
Yes.

10. If data is changed in a temporary table, does it automatically change data in the original table?
No.

11. What happens to local temporary tables after the session has been ended?
They are deleted.

12. What happens to global temporary table after the session has been ended?
If another process is still using it, the tables will be alive.

13. Which type of temporary table has a system-generated suffix attached to it? What does this suffix mean?
SQL Server allows a number of sessions to create a local temporary table with the same name without the names colliding with each other.

14. Why are inline views helpful?
You can also place a query in the FROM clause of a SELECT statement and thereby create what is called an inline view. An inline view exists only during the execution of a query. The main purpose of an inline view is to simplify the development of a one-time query. In a typical development scenario, a person would probably devise a SELECT statement, test it, examine the result, wrap it in parentheses, and continue with the development by using the inline view.

15. In SQL Server, is the ORDER BY clause allowed during the creation of a view?
No

16. Is SELECT INTO allowed in a view? Why or why not?
No. because it is a combined data definition language (DDL) and data manipulation language (DML) statement.

17. Where is the data stored in a view?
Original tabls.

18. How do you delete views?
DROP VIEW your_view_name

19. How do you delete a temporary table?
DROP TABLE ##Temp1

20. Do you need to delete a local temporary table? Why or why not?
It is not necessary. The SQL Server could remove it automatically.

21. Which operators have the highest/lowest precedence?
higest: * (multiply), / (divide), % (modulo)
lowest: = (assignment)

22. In SQL Server, if a column of FLOAT data type were divided by a column of REAL data type, what data type would the resulting column have? (Hint: refer to the section on Data Type Preference.)
FLOAT

23. Is an ORDER BY clause necessary when you use a DISTINCT? Why or why not?
No. The DISTINCT and ORDER BY do not have to be used together. When the DISTINCT is used, the ORDER BY is not necessary. DISTINCT automatically orders the result set.
 
6.7. Exercises
Unless specified otherwise, use the Student_course database to answer the following questions. Also, use appropriate column headings when displaying your output.
1. Develop and execute a query to find the names of students who had HERMANO as an instructor and earned a grade of B or better in the class. Develop the query by first finding sections where HERMANO was the instructor. Save this query. Edit the query and modify it to join the Section table with the Grade_report table. Add the grade constraint.
(1)
SELECT *
FROM Section
WHERE INSTRUCTOR = 'HERMANO'
(2)
SELECT *
FROM Section, Grade_report
WHERE INSTRUCTOR = 'HERMANO'
 and Section.SECTION_ID = Grade_report.SECTION_ID

(3)
SELECT Student.SNAME, Student.STNO, Section.SECTION_ID
FROM Section, Grade_report, Student
WHERE INSTRUCTOR = 'HERMANO'
 and Section.SECTION_ID = Grade_report.SECTION_ID
 and Grade_report.GRADE = 'B'
 and Student.STNO = Grade_report.STUDENT_NUMBER

2. Using the Student table, create a duplicate table called Stutab that contains all rows from the Student table. Hint: Look at the design of the Student table to see the columns and their definitions. Create the Stutab table with a CREATE TABLE command. Insert data into Stutab using the INSERT INTO .. SELECT option.
CREATE TABLE [dbo].[Stutab](
 [STNO] [smallint] NOT NULL,
 [SNAME] [nvarchar](20) NULL,
 [MAJOR] [nvarchar](4) NULL,
 [CLASS] [smallint] NULL,
 [BDATE] [smalldatetime] NULL,
PRIMARY KEY CLUSTERED
(
 [STNO] ASC
)
)

INSERT INTO Stutab
 SELECT * FROM Student

Using the newly created Stutab table:
a. List student names and majors of the juniors and seniors.
SELECT SNAME, MAJOR, CLASS
FROM Stutab
WHERE CLASS = 3 OR CLASS = 4

b. List student names of the COSC majors.
SELECT SNAME
FROM Stutab
WHERE MAJOR = 'COSC'

c. Create a view (call it vstu) that contains student names and majors for the COSC majors.

CREATE VIEW vstu AS
SELECT SNAME as [Name], MAJOR as [Major]
FROM Stutab
WHERE MAJOR = 'COSC'

d. List the student names and majors from vstu in descending order by name.
SELECT [name], [major]
FROM vstu
ORDER BY [name] DESC

e. Modify a row in your view of your table so that a student changes his or her major.
UPDATE vstu SET [name] = 'ZeldaNew' WHERE [name] = 'Zelda'
UPDATE vstu SET [major] = 'OTHE' WHERE [name] = 'Mary'

f. Display of the view. Did modifying the view, vstu, also change the parent table, Stutab?
Yes.

g. Try to modify the view again, but this time, change the major to COMPSC--an obviously invalid column in the Stutab table, because the column was defined as four characters. Can you do it? What happens?
It can be done sucessfully. The new view will always return empry resultset.

h. Using Stutab, create a local temporary table (call it #stutemp) that contains student names and majors for the COSC majors.
SELECT SNAME as [Name], MAJOR as [Major] INTO #stutemp
FROM Stutab
WHERE MAJOR = 'COSC'

i. List the student names and majors from #stutemp in ascending order by name.
SELECT * FROM #stutemp ORDER BY [Name] ASC

j. Modify a row in #stutemp so that a student changes his or her major.
UPDATE #stutemp SET [name] = 'AlanNew' WHERE [name] = 'Alan'

k. Display the local temporary table. Did modifying your temporary table, #stutemp, also change the parent table, Stutab.
#stutemp is changed. But the Stutab is not affected.

l. Try to modify the local temporary table again, but this time change the major to COMPSC--again, an obviously invalid field in Stutab, because the field was defined as four characters. Can you do it? What happens?
UPDATE #stutemp SET [Major] = 'AlanNew' WHERE [name] = 'AlanNew'
No. The error message:
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.

m. Using Stutab, create a global temporary table (call it ##gstutemp) that contains student names and majors for the COSC majors.
SELECT SNAME as [Name], MAJOR as [Major] INTO ##gstutemp
FROM Stutab
WHERE MAJOR = 'COSC'

n. List the student names and majors from ##gstutemp in ascending order by name.
SELECT * FROM ##gstutemp ORDER BY [Name] ASC

o. Modify a row in ##gstutemp so that a student changes his or her major.
UPDATE ##gstutemp SET [Major] = 'TEST' WHERE [Name] = 'Brad'

p. Display the global temporary table. Did modifying your temporary table, ##gstutemp, also change the parent table, Stutab.
No.

q. Try to modify the global temporary table again, but this time change the major to COMPSC--again, an obviously invalid field in Stutab, because the field was defined as four characters. Can you do it? What happens?
UPDATE ##gstutemp SET [Major] = 'TESTTTT' WHERE [Name] = 'Brad'
No. The error message:
Msg 8152, Level 16, State 4, Line 1
String or binary data would be truncated.
The statement has been terminated.

r. Create an inline view (call it invstu) that contains student names and majors for COSC majors.
SELECT * 
FROM
 ( SELECT SNAME as [Name], MAJOR as [Major] INTO ##gstutemp
FROM Stutab
WHERE MAJOR = 'COSC') AS [invstu]

3. Perform an experiment to determine the precedence in a query with three conditions linked by AND and OR. Which precedence is followed: AND, OR, or left-to-right?
Run this query:
    SELECT *
    FROM   Student
    WHERE  stno < 100 AND major = 'COSC' OR major = 'ACCT'

Then run the following two queries and determine which one gives you the same output as the preceding non parenthesized statement:
    SELECT *
    FROM   Student
    WHERE  (stno < 100 AND major = 'COSC') OR major = 'ACCT'

or:
    SELECT *
    FROM   Student
    WHERE  stno < 100 AND (major = 'COSC' OR major = 'ACCT')

What happens if you put the OR first instead of the AND and run the query without parentheses?

The first is same.
AND is higer than OR

 

4. Develop a query to find the instructor name and course name for computer science courses (use the Section table).

SELECT c.COURSE_NAME, s.INSTRUCTOR
FROM Section s, Course c
WHERE s.COURSE_NUM = c.COURSE_NUMBER
 AND c.OFFERING_DEPT = 'COSC'

 

a. Convert your query into a view.

CREATE VIEW v_Cour_Inst AS
SELECT c.COURSE_NAME, s.INSTRUCTOR
FROM Section s, Course c
WHERE s.COURSE_NUM = c.COURSE_NUMBER
 AND c.OFFERING_DEPT = 'COSC'
 
b. Convert the query into an inline view with column aliases and test it.
SELECT *
FROM
(
 SELECT c.COURSE_NAME as cname, s.INSTRUCTOR as inst
 FROM Section s, Course c
 WHERE s.COURSE_NUM = c.COURSE_NUMBER
  AND c.OFFERING_DEPT = 'COSC'
  ) AS ilvc

c. Include an ORDER BY clause outside of the inline view in the main query and run your query again.

 SELECT *
FROM
(
 SELECT c.COURSE_NAME as cname, s.INSTRUCTOR as inst
 FROM Section s, Course c
 WHERE s.COURSE_NUM = c.COURSE_NUMBER
  AND c.OFFERING_DEPT = 'COSC'
  ) AS ilvc
ORDER BY ilvc.cname  
 

 
 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息