SQL Databases Flashcards
join
joins data from 2 or more tables
what needed to join
common attribute between tables. must have tables related to each other in Common Domain
common domain
foreign key in one table on the many side correspond to primary key on the other tables(one side) share common domain
inner join
A join in which the joining condition is based on equality between values in the common columns. example customer and order table. not all customers will have order but will link between those who do. middle overlapping circles filled
join on where
when doing join on, select in the top the id on the 1 side not fk many side(multi)
what if you leave where and the two ids?
cartesian problem, you’ll have 150 rows instead of 15
inner join on
can also do that
left outer join
from customer.t left outer join order.t on
This selects all customers(left table) even if they dont have order. vin shows left circle full. middle(common) full, right side empty.
it could show customer multiple times
right outer join
from customer.t right outer join order.t on
will select all. every order. dont always need right outer join, can switch table order and do left outer join
full outer join
select all information(if no where clause
join 4 tables
where customer.t.customerID=order_t.customerid AND
order_t.orderID=orderline)t.orderid AND Orderline_t.productID=Product_t.ProductID AND Order_t.orderID=1006;
self join
rename the same table in the query. example find manager.
select e.employeeid, m.employee_name as manager
from employee as e, employee as m
where e.employeeid=m.managerid
what is subquery?
placing an inner query (select,from,where) within a where or having clause of another(outer) query
subquery example
select cusname,address,
from customer
where customerid=
(select customerid from order_t where OrderID= 10008)
what you use to select group or multiple subquery
IN use to get set of values
not in
NOT IN use to select other then the query value
another subquery
select proddesc, prodstanprice from product_t where prodstandprice> (select AVG(ProductStandPrice) from Product_t); cant do this with a join
subquery table access
you cannot access information from inner query table in outer query if that table not in outer query. they are separate
derived table
if selecting a subquery in from clause you can add a new temp. dirived table
derived table example
you can create the derived table in the from subquery and give it a single attribute(computed value) .
select ProdcutDes, prodstandprice, AvgPrice
from
(select AVG(ProductStandarPrice) FROM
Product_t) Derived_t (AvgPrice), Product_t
where productstandardprice > AvgPrice
Field Data Integrity
Default values- assume value if none present
Range Control-Allowable value limitiations
Null value control - allow or prohibit empty fields
Referentia integrity-range control, and null value allowances for foreign-key to primary-key matchups
what is denormalization
combining tables for efficiency. a large number of tables will have large number of joins potentially , computing power.
denormalization plus minus
wasted space(dup values), but less processsing , Potentials- one to one relationships and many to many with non key attributes
horizontal partitioning
Horizontal Partitioning: Distributing the rows of a table into several separate files
Each table created from partitioning has the same columns.
Useful for situations where different users need access to different rows
Example: Partitioning five year purchase data into five separate files each corresponding to a year