Primary Key & Foreign Key Implementation: MySQL

This video tutorial illustrates the implementation of Primary key and Foreign key, using MySQL.

You can run the same commands / Queries shown in the video on both phpmyadmin as well as on MySQL console.

You can also look at: Primary Foreign Unique Keys, AUTO_INCREMENT: MySQL

student-enrol-database-tables-structure

Primary Key
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys have unique values.
Primary Keys are NOT NULL by default.

Creating student table:

1
2
3
4
5
6
CREATE TABLE student(
 stud_id  int AUTO_INCREMENT,
 name     varchar(30) NOT NULL,
 age      int NOT NULL,
 PRIMARY KEY(stud_id)
);

Foreign Key
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.

Creating enrol table:

1
2
3
4
5
6
CREATE TABLE enrol(
 rol_no  int AUTO_INCREMENT,
 stud_id int NOT NULL,
 PRIMARY KEY(rol_no),
 FOREIGN KEY(stud_id) REFERENCES student(stud_id)
);

student-enrol-database-tables

Primary Key & Foreign Key Implementation: MySQL


[youtube https://www.youtube.com/watch?v=eTK9TLciRpU]

YouTube Link: https://www.youtube.com/watch?v=eTK9TLciRpU [Watch the Video In Full Screen.]



Note:
1. You can have only 1 primary key per table. But can have zero or more foreign keys in a table.
2. You can not delete primary key row directly, without deleting all the rows referring to that row.

DBMS Basics: Getting Started Guide

Getting started with Database Management System.

In this video tutorial we’re trying our best to keep everything to the minimum, and make sure not to make it look complicated for beginners.

The tables and the normalization shown in the video are just for the purpose of demonstration.

dbms-basics

DBMS is a software system which helps in managing incoming data, organizes it and provides certain ways for the data to be modified and/or extracted by the users or other programs.

Some examples of Database Management System Software include MySQL, PostgreSQL, Microsoft Access, SQL Server, FileMaker, Oracle, RDBMS, dBASE, Clipper, and FoxPro.
Also: Some Native database systems that can be connected with PHP ?

Tables Relational databases are made up of relations, commonly known as TABLES.
Relationships exists between the tables and the data are inter-related by making use of Primary and Foreign keys.

Table Columns
It has unique names.
Each column has an associated datatype.
Columns are sometimes called as fields or attributes.

Table Rows
Each row in the table represents individual data.
Rows are also called as records or tuples.

Values
Every value must have the same data type, as specified by it’s column.

Key
To identify each row uniquely.
The identifying column in a table is called as key or primary key.

Schema
Complete set of table design for a database.
It’s like a blueprint for the database.

A Schema should show the tables along with their columns, and the primary and foreign keys. Usually primary key’s are underlines and foreign keys are italicized.

Database Management System: Basics


[youtube https://www.youtube.com/watch?v=aR44FbeeFH8]

YouTube Link: https://www.youtube.com/watch?v=aR44FbeeFH8 [Watch the Video In Full Screen.]



Anomalies
Assume that we’re running an online store.
We’ve a order table.

If a person called Satish orders Apple iPad, Mac Book and a iPhone from our site.
We store his name and address and quantity of his order.

Next, Satish moves to a different place before we process the order, now we will need to update his address at 3 places!

Doing 3 times as much work. We may miss updating Satish’s address in some place, making the data inconsistent. This is called modification anomaly.

If we design our database table in this way, we’ll need to take the address of Satish each time he orders something from our online store. This way, we need to always make sure the address(data) is consistent across all the rows. If we do not take care, we may end up with conflicting data.
Ex: One row may indicate Satish to be living in Bangalore and another row may indicated Satish to be living in New York!
This scenario is called Insertion anomaly.

Once all the orders of Satish has been processed, we delete the records. This way, we no longer have Satish’s address. So we can’t send any promotional offers etc to Satish in the future. If he want’s to order something again from our online store, he need to enter his address again. This scenario is called deletion anomaly.

We could solve these anomaly problems by making use of Primary and Foreign key’s and by developing the skill/art of normalization.

Database normalization is the process of organizing the fields and tables of a relational database to minimize redundancy and dependency. Normalization usually involves dividing large tables into smaller (and less redundant) tables and defining relationships between them. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database using the defined relationships.

Primary Key
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.

Foreign Key
A foreign key is a field in a relational table that matches a candidate key of another table. The foreign key can be used to cross-reference tables.

Relationships
We’ll cover
one-one
one-many
many-many
relationships in coming video tutorials, with some real time examples.

Per-Class Constants And Static Methods: PHP OOP

This video tutorial illustrates the concepts of Per-Class Constants and Static Methods in Object Oriented PHP. Per-Class Constants was introduced in PHP5.

Per-Class Constants
Unlike variables, constants doesn’t change/alter its value during the program execution.
They’re treated as fixed values. Something which doesn’t change in the course of execution.

Since its value remains constant for all the objects, there is no need of an object to be created in-order to access it. We can directly access it using the class name and :: operator and the constant name.

Per-Class Constant
Math.php

1
2
3
4
5
6
7
< ?php
class Math {
const pi = 3.14159;
}
 
       echo Math::pi;
?>

This would output, 3.14159

Static Method
There’re situations where we need to take something which is global to the class.
And there is no use of invoking the method for all objects of that class. In such a situation make use of static methods. This way, you need not instantiate the class to invoke the method.

Syntax to invoke static method: class name, :: operator, method name.

Static Method Invocation
Math.php

1
2
3
4
5
6
7
8
9
< ?php
class Math {
static function square($no)
{
echo "<br />".$no * $no;
}
                echo Math::square(10);
}
?>

This would output, 100

Per-Class Constants And Static Methods: PHP OOP


[youtube https://www.youtube.com/watch?v=0tpX377CEgw]

YouTube Link: https://www.youtube.com/watch?v=0tpX377CEgw [Watch the Video In Full Screen.]



For example,
Declare database connection method as static, so that you can directly use the class name instead of creating an instance of the class.
This way, you can connect to the database, but you don’t have access to other non-static methods of the same class!

Implementing Interfaces: PHP OOP

Video tutorial illustrates the concept of implementing interfaces in Object Oriented PHP.

We also show you, how it over comes the ambiguity issue of multiple inheritance.

Interfaces acts as placeholder or the blueprint for what you can create.
Interfaces provide the basic design for a class with zero implementation detail.
Interfaces define what methods a class must have ..and you can create other methods outside of these limitations too.

extend and implement
interface.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
< ?php
class A
{
public function display()
{
echo "I love Apple Products";
}
}
 
interface I
{
const COMPANY = "Technotip.com";
public function company();
public function display();
}
 
class B extends A implements I
{
function company()
{
echo "I Love BuySellAds and Technotip.com";
}
 
function display()
{
echo "Microsoft & Oracle";
}
}
 
$obj = new B();
$obj->company();
?>

This would output I Love BuySellAds and Technotip.com

Accessing Constant Variable
To access the constant variable present inside interface, use the class name followed by scope resolution operator and then the constant variable name:
B::COMPANY

Implementing Interfaces: PHP OOP


[youtube https://www.youtube.com/watch?v=iOfpmXph5EU]

YouTube Link: https://www.youtube.com/watch?v=iOfpmXph5EU [Watch the Video In Full Screen.]



In multiple inheritance you have two parent classes. If both of them have a method with same name, signature and you don’t even override it in the child / derived class, then PHP engine will have no way to know which method to use.

multiple-inheritance-classB-classC-to-classA


In interface, you HAVE TO (must) override the methods present in interface class while implementing it. Hence solving the ambiguity issue.

Note:
All methods declared in an interface must be public.
All variables in interface must be constant.
Interfaces cannot be instantiated.
Object Oriented PHP only supports Single Inheritance. i.e., you can have only one parent class.
You can implement more than one interface. Use comma separation to implement more than one interfaces.

Ex 1:
interface A { }
interface B { }
class C implements A, B { }

Ex 2:
class D { }
interface A { }
interface B { }
class C extends D implements A, B { }

Multiple And Hybrid Inheritance: PHP OOP

Video tutorial to illustrate Inheritance mechanism in Object Oriented PHP.

Inheritance is a mechanism where objects of one class acquires the properties of objects of another class.

PHP supports only single inheritance.
i.e., a class should inherit from only one parent class / base class. If it inherits from more than one base class, then it’s not a valid inheritance in PHP.

Thus, multiple inheritance and hybrid inheritance is not supported in Object Oriented PHP.

Single Inheritance
class B inherits from class A

single-inheritance-classA-to-classB

class C inherits from class B which in turn inherits from class A

single-inheritance-classA-to-classB-to-classC

class B and class C inherits from class A

single-inheritance-classA-to-classB-and-classC

Invalid Inheritance in PHP
Multiple Inheritance
class A inherits from two base/parent classes B and C

multiple-inheritance-classB-classC-to-classA

Hybrid Inheritance
class B and C inherits from a single base/parent class, i.e., class A
again, class D inherits from class B and class C

hybrid-inheritance

Single Inheritance: class B and class C inherits from class A

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
< ?php
class A
{
function display()
{
echo "Apple, Google, Oracle, Microsoft";
}
}
 
class B extends A
{
 
}
 
class C extends A
{
 
}
 
$obj = new C(); 
//$obj = new A(); 
//$obj = new B(); 
$obj->display();
?>

This should output
Apple, Google, Oracle, Microsoft

Multiple And Hybrid Inheritance: PHP OOP


[youtube https://www.youtube.com/watch?v=vEubH2KVgYk]

YouTube Link: https://www.youtube.com/watch?v=vEubH2KVgYk [Watch the Video In Full Screen.]



Try yourselves:
If you try to extend class A and class B to class C, it’ll through fatal error.

Related Read:
Single Inheritance: PHP OOP

Note:
So, technically you can’t extend/inherit from more than 1 base/parent class.
To overcome this situation, we can make use of Interfaces.
Interfaces in PHP works similar to that of Java. We’ll teach interfaces in another video tutorial, so stay subscribed.