Tuesday, May 31, 2016

WDP Database Queries - Exercise

Identify the purpose and syntax and provide an example code and output for each of the query.


  • SELECT
Purpose: Retrieve or extract data from a database.

SQL SELECT Syntax

SELECT column_name,column_name
FROM table_name;
and
SELECT * FROM table_name;
Retrieve from all columns 
>SELECT * FROM <tables>
>SELECT * FROM teachers;

Retrieve from selected columns 
>SELECT <column 1>,  <column 2 FROM <table>

Retrieve unique values 
>SELECT DISTINCT <columnName FROM <table>

Retrieve data satisfying a given condition 
>SELECT <column FROM <tablesWHERE <condition>

Retrieve data satisfying multiple conditions 
>SELECT <columnFROM  <tablesWHERE <condition> AND <condition>


Other SELECT purposes

  • JOIN
Purpose: used to combine rows from two or more tables, based on a common field between them.

Different SQL JOINs
Before we continue with examples, we will list the types of the different SQL JOINs you can use:
  • INNER JOIN: Returns all rows when there is at least one match in BOTH tables
  • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
  • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
  • FULL JOIN: Return all rows when there is a match in ONE of the tables

The syntax of the MySQL INNER JOIN clause is as follows:

Example




SELECT 
    productCode, productName, textDescription
FROM
    products T1
        INNER JOIN
    productlines T2 ON T1.productline = T2.productline;


  • INSERT INTO
Purpose: The INSERT INTO statement is used to insert new records in a table.

SQL INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two forms.
The first form does not specify the column names where the data will be inserted, only their values:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
 INSERT INTO teachers (id, name, age) VALUES (NULL, ‘John’, ‘12’);
  • UPDATE
Purpose: UPDATE statement is used to update records in a table.


SQL UPDATE Syntax


UPDATE table
SET column1 = expression1,
    column2 = expression2,
    ...
[WHERE conditions];
OR
The syntax for the SQL UPDATE statement when updating a table with data from another table is:
UPDATE table1
SET column1 = (SELECT expression1
               FROM table2
               WHERE conditions)
[WHERE conditions];
  • DELETE
Purpose: The DELETE statement is used to delete records in a table.

Syntax
The syntax for the DELETE statement in SQL is:
DELETE FROM table
[WHERE conditions];

  • DROP
Purpose: The SQL DROP TABLE statement allows you to remove or delete a table from the SQL database.


DROP INDEX Syntax for MySQL:

ALTER TABLE table_name DROP INDEX index_name

OR

DROP TABLE table_name;

  • CREATE TABLE
Purpose: The SQL CREATE TABLE statement allows you to create and define a table.

Syntax

The syntax for the CREATE TABLE statement in SQL is:
CREATE TABLE table_name
( 
  column1 datatype [ NULL | NOT NULL ],
  column2 datatype [ NULL | NOT NULL ],
  ...
);

Wednesday, May 18, 2016

VLSM Exercise

Exercise 3: We want to divide 192.168.10.0 which is class C network, into four networks, each with unequal number of IP address requirement as shown below:
  •  Subnet A: 32 host.
  • Subnet B: 8 host.
  • Subnet C: 22 host.
  • Subnet D: 60 hosts.

Solution:
Organize, high - low
·         Subnet D: 60 hosts.
Host = 26 = 64 – 2 = 62                      N = 22 = 64
·         Subnet A: 32 host.
Host = 26 = 64 – 2 = 62                      N = 22 = 64
·         Subnet C: 22 host.
Host = 25 = 32 – 2 = 30                      N = 23 = 32
·         Subnet B: 8 host.
Host = 24 = 16 – 2 = 14                      N = 24 = 16


Subnet
Network Name
Network Address
Valid Host Range
Broadcast Address
Subnet Mask
22 = 64
D
192.168.10.0
192.168.10.1 - 192.168.10.62
192.168.10.63
255.255.255.192
22 = 64
A
192.168.10.64
192.168.10.65 - 192.168.10.126
192.168.10.127
255.255.255.192
23 = 32
C
192.168.10.128
192.168.10.129 - 192.168.10.158
192.168.10.159
255.255.255.224
24 = 16
B
192.168.10.160
192.168.10.161 - 192.168.10.174
192.168.10.175
255.255.255.240




Exercise 4: Given network of 201.4.3.0/24, subnet the network in order to create the subnetworks with the following:
  • Office 1 – 14 hosts.
  • Office 2 – 60 hosts.
  • Office 3 – 32 hosts.
  • Office 4 – 7 hosts.
  • Office 5 – 15 hosts.


Solution:
Organize, high - low
·         Office 2 – 60 hosts.
Host = 26 = 64 – 2 = 62                      N = 22 = 64
·         Office 3 – 32 hosts.
Host = 26 = 64 – 2 = 62                      N = 22 = 64
·         Office 5 – 15 hosts.
Host = 25 = 32 – 2 = 30                      N = 23 = 32
·         Office 1 – 14 hosts.
Host = 24 = 16 – 2 = 14                      N = 24 = 16
·         Office 4 – 7 hosts.
Host = 24 = 16 – 2 = 14                      N = 24 = 16


Subnet
Network Name
Network Address
Valid Host Range
Broadcast Address
Subnet Mask
22 = 64
2
192.168.10.0
192.168.10.1 - 192.168.10.62
192.168.10.63
255.255.255.192
22 = 64
3
192.168.10.64
192.168.10.65 - 192.168.10.126
192.168.10.127
255.255.255.192
23 = 32
5
192.168.10.128
192.168.10.129 - 192.168.10.158
192.168.10.159
255.255.255.224
24 = 16
1
192.168.10.160
192.168.10.161 - 192.168.10.174
192.168.10.175
255.255.255.240
24 = 16
4
192.168.10.176
192.168.10.176 - 192.168.10.190
192.168.10.191
255.255.255.240

WDP - Database

Database

Databasealso called electronic database, any collection of data, or information, that is specially organized for rapid search and retrieval by a computer.

A relational database (RDB) is a collective set of multiple data sets organized by tables, records and columns.


Relational Database


What is SQL?

  • SQL is a standard language for accessing and manipulating databases.
  • SQL stands for Structured Query Language
  • SQL lets you access and manipulate databases
  • SQL is an ANSI (American National Standards Institute) standard

SQL Capabilities

  • SQL can execute queries against a database
  • SQL can retrieve data from a database
  • SQL can insert records in a database
  • SQL can update records in a database
  • SQL can delete records from a database
  • SQL can create new databases
  • SQL can create new tables in a database
  • SQL can create stored procedures in a database
  • SQL can create views in a database
  • SQL can set permissions on tables, procedures, and views


3-tier Architecture

A 3-tier architecture separates its tiers from each other based on the complexity of the users and how they use the data present in the database. It is the most widely used architecture to design a DBMS.


  1. Database (Data) Tier − At this tier, the database resides along with its query processing languages. We also have the relations that define the data and their constraints at this level.
  2. Application (Middle) Tier − At this tier reside the application server and the programs that access the database. For a user, this application tier presents an abstracted view of the database. End-users are unaware of any existence of the database beyond the application. At the other end, the database tier is not aware of any other user beyond the application tier. Hence, the application layer sits in the middle and acts as a mediator between the end-user and the database.
  3. User (Presentation) Tier − End-users operate on this tier and they know nothing about any existence of the database beyond this layer. At this layer, multiple views of the database can be provided by the application. All views are generated by applications that reside in the application tier.
Multiple-tier database architecture is highly modifiable, as almost all its components are independent and can be changed independently.
phpMyAdmin is a free software tool written in PHP, intended to handle the administration of MySQL over the Web. Frequently used operations (managing databases, tables, columns, relations, indexes, users, permissions, etc) can be performed via the user interface, while you still have the ability to directly execute any SQL statement.

Installing phpMyadmin
Important Notice: The application must be installed within the wwws directory.
Follow the steps below to install the phpMyAdmin program into your account on Webserve:
  1. Login to your Webserve account using an SSH command line client such as PuTTY. For further instructions on this step, see: How to use PuTTY.
  2. Move into your wwws directory by typing:
    webserve:account> cd wwws
    If your account does not have a 'wwws' directory, refer to the Secure WWW Server document for information on creating this necessary directory.
  3. Install phpMyAdmin by typing the following:
    webserve:account> install_phpmyadmin
    An .htaccess file similar to the one below is automatically created in the phpmyadmin directory.
    AuthGroupFile /dev/null
    AuthType CAS
    AuthName "IU Network ID"
    <Limit GET POST>
    require user account
    </Limit>                                                                      
    You should not delete this .htaccess file. Removing it will make your MySQL database vulnerable to exploit.
    This .htaccess file currently restricts access to the IU Network ID 'account' where 'account' is the name of your Webserve account. You can edit this .htaccess file to add additional users to the line (separate each name with a space):
    <Limit GET POST>                                                                           
    require user account username1 username2                                                        
    </Limit>
  4. You will need to edit the config.inc.php file before running phpMyAdmin. To move into the directory where this file is located, type: cd phpmyadmin
    As you edit the file, make sure lines do not wrap as a result of your edits. If you notice the line wraps as you enter information, correct the line so that it does not wrap.
    Below is the section of the config.inc.php file that you need to edit in order to establish a database connection. Replace port numberusername, and password as shown below with the port number for your account and the username/password that you've granted privileges to when setting up your database.
    $cfg['Servers'][$i]['host'] = 'mysql.iu.edu'; // MySQL hostname
    $cfg['Servers'][$i]['port'] = 'port number'; // MySQL port 
    $cfg['Servers'][$i]['extension'] = 'mysql';
    $cfg['Servers'][$i]['connect_type']= 'tcp';
    $cfg['Servers'][$i]['compress'] = false;
    $cfg['Servers'][$i]['auth_type'] = 'config';
    $cfg['Servers'][$i]['user'] = 'username'; // MySQL user
    $cfg['Servers'][$i]['password'] = 'password'; // MySQL password 
    By default, the config.inc.php file has three blocks: one for connecting to the MySQL production server (mysql.iu.edu), one for connecting to the test MySQL server (mysql-test.iu.edu) and a blank one. The test server and blank ones are commented out so if you wish to establish connections beyond the first block, you will need to remove the "//" as these serve to "comment out" that portion of the file. Below is an example of the block with the comment tags still present.
    //$cfg['Servers'][$i]['host']        = 'mysql-test.iu.edu';//MySQL hostname
    //$cfg['Servers'][$i]['port']        = ''; // MySQL port
    //$cfg['Servers'][$i]['extension']   = 'mysql';
    //$cfg['Servers'][$i]['connect_type']= 'tcp';
    //$cfg['Servers'][$i]['compress']    = FALSE;
    //$cfg['Servers'][$i]['auth_type']   = 'config';
    //$cfg['Servers'][$i]['user']        = ''; // MySQL user
    //$cfg['Servers'][$i]['password']    = ''; // MySQL password

How to connect to the database (PHP)

Open a Connection to MySQL

Before we can access data in the MySQL database, we need to be able to connect to the server:

Example (MySQLi Object-Oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";
?>


Example (MySQLi Procedural)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Close the Connection

The connection will be closed automatically when the script ends.

Example (MySQLi Procedural)

mysqli_close($conn);
This is a very important function as it closes the connection to the database server. Your script will still run if you do not include this function. And too many open MySQL connections can cause problems for your account.

Referrences:
  • http://www.britannica.com/technology/database
  • https://www.techopedia.com/definition/1234/relational-database-rdb
  • http://www.w3schools.com/sql/sql_intro.asp
  • http://www.tutorialspoint.com/dbms/dbms_architecture.htm
  • https://www.phpmyadmin.net/
  • http://webmaster.iu.edu/tools-and-guides/mysql/phpmyadmin.phtml