SQL Joins
Upcoming SlideShare
Loading in...5
×
 

SQL Joins

on

  • 229 views

 

Statistics

Views

Total Views
229
Slideshare-icon Views on SlideShare
229
Embed Views
0

Actions

Likes
0
Downloads
5
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    SQL Joins SQL Joins Document Transcript

    • Some of these notes are specific to PHP however the concepts typically apply to any OOPlanguage. For most programmers these concepts are probably already embedded in theirbrains. For those of us who weren’t computer science majors or have been stuck on a projector framework for an extended period of time this will be a good refresher as most employerswill ask questions related to these concepts in the early stages of an interview or technicalevaluation.Different SQL JOINsLEFT JOIN: Return all rows from the left table, even if there are no matches in the right tableRIGHT JOIN: Return all rows from the right table, even if there are no matches in the left tableINNER JOIN: produces only the set of records that match in both Table A and Table B. SELECT * FROM TableA INNER JOIN TableB ON TableA.name = TableB.nameFULL OUTER JOIN: produces the set of all records in Table A and Table B, with matchingrecords from both sides where available. If there is no match, the missing side will contain null. SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.nameLEFT OUTER JOIN: produces a complete set of records from Table A, with the matchingrecords (where available) in Table B. If there is no match, the right side will contain null. SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.name = TableB.nameTo produce the set of records only in Table A, but not in Table B, we perform the same left outerjoin, then exclude the records we dont want from the right side via a where clause. SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.name = TableB.name WHERE TableB.id IS nullTo produce the set of records unique to Table A and Table B, we perform the same full outerjoin, then exclude the records we dont want from both sides via a where clause.
    • SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.name WHERE TableA.id IS null OR TableB.id IS nullStatic KeywordDeclaring class properties or methods as static makes them accessible without needing aninstantiation of the class. A property declared as static can not be accessed with an instantiatedclass object (though a static method can).For compatibility with PHP 4, if no visibility declaration is used, then the property or method willbe treated as if it was declared as public.Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.Static properties cannot be accessed through the object using the arrow operator ->.Calling non-static methods statically generates an E_STRICT level warning.Like any other PHP static variable, static properties may only be initialized using a literal orconstant; expressions are not allowed. So while you may initialize a static property to an integeror array (for instance), you may not initialize it to another variable, to a function return value, orto an object.As of PHP 5.3.0, its possible to reference the class using a variable. The variables value cannot be a keyword (e.g. self, parent and static).VisibilityThe visibility of a property or method can be defined by prefixing the declaration with thekeywords public, protected or private. Class members declared public can be accessedeverywhere.Protected - can be accessed only within the class itself and by inherited and parent classes.Private - may only be accessed by the class that defines the member.Public - can be accessed from anywhereProperty VisibilityClass properties must be defined as public, private, or protected. If declared using var withoutan explicit visibility keyword, the property will be defined as public.
    • Method VisibilityClass methods may be defined as public, private, or protected. Methods declared without anyexplicit visibility keyword are defined as public.Visibility from other objectsObjects of the same type will have access to each others private and protected members eventhough they are not the same instances. This is because the implementation specific details arealready known when inside those objects.Abstract Classes & InterfacesAbstract ClassesAn abstract class is a class that is only partially implemented by the programmer. It may containone or more abstract methods. An abstract method is simply a function definition that serves totell the programmer that the method must be implemented in a child class.InterfacesAn interface is similar to an abstract class; indeed interfaces occupy the same namespace asclasses and abstract classes. For that reason, you cannot define an interface with the samename as a class. An interface is a fully abstract class; none of its methods are implemented andinstead of a class sub-classing from it, it is said to implement that interface. An interface definesa contract that implementing classes must fulfill. An interface definition consists of signatures ofpublic members, without any implementing code.Let’s say you had three classes - BedroomDoor, Gate and GarageDoor. Lets also imagine theBedroomDoor and GarageDoor (and all other Doors) have a range of varying behaviour, butboth have exactly the same locking mechanisms. So we have: PHP Code: class Gate { // gate specific behaviour } class BedroomDoor { // bedroom door specific behaviour public function lock($key) { $this->lock->insert_key($key); $key->turn(clockwise); }
    • }class GarageDoor { // garage door specific behaviour public function lock($key) { $this->lock->insert_key($key); $key->turn(clockwise); }}Now, as you can see the two lock methods are exactly the same - duplication is evil!All Doors can be locked in the same way (but not Gates, these are bolted shut). We caneliminate this duplication by introducing an abstract class Door which implements thislock method for all of the concrete instances, eliminating the duplication.PHP Code:abstract class Door { public function lock($key) { $this->lock->insert_key($key); $key->turn(clockwise); }}class BedroomDoor extends Door { // bedroom door specific behaviour}class GarageDoor extends Door { // garage door specific behaviour}Note that we dont necessarily have to use an abstract class to eliminate theduplication, we could just inherit from a normal concrete Door class that can beinstantiated in its own right, depending on whether it makes sense for a Door to beinstantiated on its own.Now, interfaces - interfaces can be used to specify a specific set of behaviour and thatbehaviours API which classes can implement. As explained above, interfaces are bestlooked at as adjectives. Both doors and gates can be opened. Thats an interface rightthere:PHP Code:interface Openable { public function open() { }
    • }Both doors (through the abstract Door class) and the gate can be opened, so they canimplement the interface.PHP Code:class Gate implements Openable { public function open() { if($this->is_locked()) { $this->unlock(); } $this->leftSide()->pullOpen(); $this->rightSide()->pullOpen(); }}abstract class Door implements Openable { // our abstract class defines the open function // as abstract as we open different types of door // differently abstract public function open() {}}class GarageDoor { public function open() { // implement here }}class BedroomDoor { public function open() { // implement here }}By specifying that we will implement the Openable interface, the class is bound by thatcontract and you will get an error if you forget to implement any of the methods in theinterface. Finally, you can take advantage of type hinting in your methods - lets say youneed to pass an object into another class to do something - the receiving class doesntcare what object we pass it as long as it can be opened. We can enforce this using typehinting:PHP Code:class Foo { public function openSomething(Openable $arg) { $arg->open(); }}Doing the above ensures that whatever gets passed to the openSomething() methodwill respond to open() because it implements the Openable interface.
    • Error vs ExceptionExceptions are thrown intentionally by code using a throw, errors... not so much.Errors come about as a result of something which isnt handled typically. (IO errors, TCP/IPerrors, null reference errors)Pass by value vs Pass by referencePass by value - A copy of the value of your variable is made, and that copy is passed to thefunction you have called.A function or subroutine that is passed a variable by value is working with a copy of thatvariables’ contents. The original variables’ contents are not affected by what happens inside ofyour subroutine. The scope of the copy is restricted to the lifetime of your subroutine. Upon exit,the copy will likely be taken care of by garbage collection unless you explicitly take some sort ofaction to keep it around, such as returning its value to caller.Pass by references - A reference to location of the original variables’ contents is passed to thefunction.A function that takes a value by reference is given the address of the location in memory wherethe variables’ value can be found. Any statements in the function then operate on the originalvariables’ contents. Changes made will persist after your function or subroutine has run itscourse.