Академический Документы
Профессиональный Документы
Культура Документы
___________________________________________________________________________________________________
Copyrights Reserved Page 1 of 10
Chapter Includes:
___________________________________________________________________________________________________
Copyrights Reserved Page 2 of 10
Adding columns to a table
We use Alter table command for adding and removing the columns to the
table.
Column Properties:
Renaming objects
Temporary tables
Temporary tables are the real tables created in the tempdb database,
usually for the purpose of holding an intermediate result set. They can be
identified by the symbol #.
They exist for a particular session.
___________________________________________________________________________________________________
Copyrights Reserved Page 3 of 10
Temporary tables:
Temporary tables only last as long as the session exists and they are
available only for your session.
They exist til the explicitly is dropped or the SQL server is restarted.
Views
Views are logical ways of looking at the physical data located in the tables.
It is used to limit access to selected columns in a base table. We can also
limit the access to rows by using view with where clause, which is referred
as view horizontal.
View with check option prevents the insertion or updating of rows that will
subsequently not meet the view criteria.
Syntax : create view view name as select col_name from table name
Rule
___________________________________________________________________________________________________
Copyrights Reserved Page 4 of 10
Defaults
Defaults provide a value for a column when one is not supplied at insert
time. They exist only as database objects, after creating, they must be
subsequently bound to columns.
Syntax : create default default_name as expression.
User defined datatypes are not really a new data type, it is a way of
describing an existing data type. it provides a mechanism for enforcing data
type consistency across and within a database or server.
Syntax : sp_add type type_name sys name
Constraints
___________________________________________________________________________________________________
Copyrights Reserved Page 5 of 10
Primary key and Unique key constraints:
They require that all values in a table be unique and the columns cannot
allow null values. Where as a Unique key constraint can allow one null value
in its respective columns.
Check constraints:
Similar to rules they specify a domain for columns. They are slightly faster.
They are defined for a table using the create table or alter table command.
They can be defined at the column level or table level.
It is the property of all foreign keys having associated with a primary key in
their related tables. Constraints which enforce both primary and foreign key
integrity checks without programming is called Declarative referential
integrity.
Important Syntaxes
a) Altering a table
It means adding a new column to a table with the help pf alter command.
b) Renaming an objects
Sp_rename 'table_name.old_name',new_col_name
___________________________________________________________________________________________________
Copyrights Reserved Page 6 of 10
c) Views
d) Rules
e) Default
Default provide a value for a column when one is not supplied at the
insertion time
SESSION:
Create Rule that if matches lost=3 then points= -3 and matches lost= 6
then points=-6
Create default that rank should be null if the points less than 0
___________________________________________________________________________________________________
Copyrights Reserved Page 7 of 10
3) What happens when we fail to specify a common key while using join ?
4) What are user defined data types ? How many valid data types are used for
storing strings?
Distinct is the only keyword that removes duplicate rows from the result set.
8) What are foreign-key constraints ? How can u modify and add constraints?
___________________________________________________________________________________________________
Copyrights Reserved Page 8 of 10
DATABASE SCHEMA
Product(maker,model,type)
PC(code,model,speed,ram,hd,cd,price)
Laptop(code,model,speed,ram,hd,screen,price)
Printer(code,model,color,type,price)
The relation "Product" shows the maker, model number, and type (pc, laptop, or
printer). It is assumed that model numbers are unique for all the makers and
product types. Each model number specifying pc in the relation "PC" is
characterized by speed (of the processor in MHz), total amount of RAM (in Mb),
hard disk drive capacity (in Gb), CD ROM speed (for example, '4x'), and the price.
The relation "Laptop" is similar to that one of PCs except for the CD ROM speed,
which is replaced by the screen size (in inches). For each printer model in the
relation "Printer" it is told whether the printer is color or not (color attribute is 'y'
for color printers; otherwise it is 'n'), printer type (laser, jet, or matrix), and the
price.
___________________________________________________________________________________________________
Copyrights Reserved Page 9 of 10
Based on the above database scheme solve the following Queries
1. Find the model number, speed and hard drive capacity for all the PCs with
prices below $500.
Result set: model, speed, hd.
3. Find the model number, speed and hard drive capacity of the PCs having 12x
CD and prices less than $600 or having 24x CD and prices less than $600.
Topics Covered:
by
Ray Rankins
Jeffrey R. Garbus
David Solomon
Bennett Wm.McEwan
___________________________________________________________________________________________________
Copyrights Reserved Page 10 of 10