Oss-Unit-III Up
Oss-Unit-III Up
VANIYAMBADI
PG and Department of Computer Applications
III BCA – Semester – VI
E-Notes (Study Material)
Learning Objectives:
1.To understand the concept of HTML, HTML5 and CSS.
2.To understand the concept of scripting code for a website.
3.To understand the concept of scripting code for a website.
4.To understand the fundamentals of PHP language combined with HTML.
5.To understand the fundamentals of PERL languages.
Course Outcome:
1.The student will be able to understand the concept of HTML, HTML5 and CSS.
2.The student will be able to learn to inspect and detect errors by going through each and every code
segment.
3.The student will be able to understand basic concept of Java script and MYSQL.
4.The student will be able to understand basic concept of PHP.
5.The student will be able to understand basic concept of PERL.
1. Advantages of JavaScript:
The merits of using JavaScript are −
Less server interaction − You can validate user input before sending the page off to the
server. This saves server traffic, which means less load on your server.
Immediate feedback to the visitors − They don't have to wait for a page reload to see if
they have forgotten to enter something.
Increased interactivity − You can create interfaces that react when the user hovers over
them with a mouse or activates them via the keyboard.
Richer interfaces − You can use JavaScript to include such items as drag-and-drop
components and sliders to give a Rich Interface to your site visitors.
Limitations of JavaScript
Client-side JavaScript does not allow the reading or writing of files. This has been kept for
security reason.
JavaScript cannot be used for networking applications because there is no such support
available.
JavaScript doesn't have any multi-threading or multiprocessor capabilities.
History of Java Script:
JavaScript Versions
Let’s take a look at the different versions of ECMAScript, their release years, and the key features
they introduced
Release
Version Name Year Features
Added:
Regular Expression
try/catch
Exception Handling
ES3 ECMAScript 3 1999 switch case and do-while
Added:
JavaScript “strict mode”
JSON support
ES5 ECMAScript 5 2009 JS getters and setters
Added:
let and const
Class declaration
import and export
ECMAScript for..of loop
ES6 2015 2015 Arrow functions
Added:
Block scope for variable
async/await
ECMAScript Array.includes function
ES7 2016 2016 Exponentiation Operator
Added:
Object.values
ECMAScript Object.entries
ES8 2017 2017 Object.getOwnPropertiesDescriptors
Added:
ECMAScript spread operator
ES9 2018 2018 rest parameters
Added:
Array.flat()
ECMAScript Array.flatMap()
ES10 2019 2019 Array.sort is now stable
Added:
ECMAScript BigInt primitive type
ES11 2020 2020 nullish coalescing operator
Added:
ECMAScript String.replaceAll() Method
ES12 2021 2021 Promise.any() Method
Added:
Top-level await
ECMAScript New class elements
ES13 2022 2022 Static block inside classes
Added:
toSorted method
toReversed method
findLast, and findLastIndex
ECMAScript methods on Array.prototype and
ES14 2023 2023 TypedArray.prototypet
Additional Resources:
https://www.geeksforgeeks.org/introduction-to-javascript/
Practice Questions:
1.What are the advantages of JavaScript?
2.Define JavaScript.
2.JavaScript Syntax:
JavaScript can be implemented using JavaScript statements that are placed within the <script>...
</script> HTML tags in a web page.
You can place the <script> tags, containing your JavaScript, anywhere within your web page, but
it is normally recommended that you should keep it within the <head> tags.
The <script> tag alerts the browser program to start interpreting all the text between these tags as
a script. A simple syntax of your JavaScript will appear as follows.
<script ...>
JavaScript code
</script>
The script tag takes two important attributes −
Language − This attribute specifies what scripting language you are using. Typically, its
value will be javascript. Although recent versions of HTML (and XHTML, its successor)
have phased out the use of this attribute.
Type − This attribute is what is now recommended to indicate the scripting language in use
and its value should be set to "text/javascript".
So your JavaScript segment will look like −
<script language = "javascript" type = "text/javascript">
JavaScript code
</script>
JavaScript comments can also be used to prevent execution, when testing alternative code.
Any text between // and the end of the line will be ignored by JavaScript (will not be executed).
Example
// Change heading:
document.getElementById("myH").innerHTML = "My First Page";
// Change paragraph:
document.getElementById("myP").innerHTML = "My first paragraph.";
This example uses a single line comment at the end of each line to explain the code:
Example
let x = 5; // Declare x, give it the value of 5
let y = x + 2; // Declare y, give it the value of x + 2
Example:
<!DOCTYPE html>
<html>
<body>
<h1 id="myH"></h1>
<p id="myP"></p>
<script>
// Change heading:
document.getElementById("myH").innerHTML = "JavaScript Comments";
// Change paragraph:
document.getElementById("myP").innerHTML = "My first paragraph.";
</script>
</body>
</html>
OUTPUT:
JavaScript Comments
My first paragraph.
Multi-line Comments
Multi-line comments start with /* and end with */.
Any text between /* and */ will be ignored by JavaScript.
This example uses a multi-line comment (a comment block) to explain the code:
Example
/*
The code below will change the heading with id = "myH"
and the paragraph with id = "myP" in my web page:
*/
document.getElementById("myH").innerHTML = "My First Page";
document.getElementById("myP").innerHTML = "My first paragraph.";
Example:
<!DOCTYPE html>
<html>
<body>
<h1 id="myH"></h1>
<p id="myP"></p>
<script>
/*
The code below will change
the heading with id = "myH"
and the paragraph with id = "myP"
*/
document.getElementById("myH").innerHTML = "JavaScript Comments";
document.getElementById("myP").innerHTML = "My first paragraph.";
</script>
</body>
</html>
Output:
JavaScript Comments
My first paragraph.
The general rules for constructing names for variables (unique identifiers) are:
Names can contain letters, digits, underscores, and dollar signs.
Names must begin with a letter.
Names can also begin with $ and _ (but we will not use it in this tutorial).
Names are case sensitive (y and Y are different variables).
Reserved words (like JavaScript keywords) cannot be used as names.
Additional Resources:
https://www.w3schools.com/js/js_variables.asp
Practice Questions:
1. Explain basic structure of java script with example.
2. Write a syntax for Java script?
3. Explain comments in java script?
objects, arrays, dates, maps, sets, intarrays, floatarrays, promises, and more.
Examples
// Numbers:
let length = 16;
let weight = 7.5;
// Strings:
let color = "Yellow";
let lastName = "Johnson";
// Booleans
let x = true;
let y = false;
// Object:
const person = {firstName:"John", lastName:"Doe"};
// Array object:
const cars = ["Saab", "Volvo", "BMW"];
// Date object:
const date = new Date("2022-03-25");
Note − JavaScript does not make a distinction between integer values and floating-point values.
All numbers in JavaScript are represented as floating-point values. JavaScript represents numbers
using the 64-bit floating-point format defined by the IEEE 754 standard.
Additional Resources:
https://www.w3schools.com/js/js_datatypes.asp
Practice Questions:
1.List any four data types in JavaScript.
2.What is the difference between null and undefined in JavaScript?
3.Write a program that demonstrates the use of all primitive data types in JavaScript.
4.JavaScript Variables:
Like many other programming languages, JavaScript has variables. Variables can be
thought of as named containers. You can place data into these containers and then refer to the data
simply by naming the container.
Before you use a variable in a JavaScript program, you must declare it. Variables are declared with
the var keyword as follows.
double In super
Additional Resources:
https://www.w3schools.com/js/js_datatypes.asp
Practice Questions:
1.Create a program to demonstrate the block scope and global scope of variables in JavaScript.
2.Discuss in detail the impact of using global variables in a program and how they differ from
local variables.
5.JavaScript Array
JavaScript array is an object that represents a collection of similar type of elements.
There are 3 ways to construct array in JavaScript
By array literal
By creating instance of Array directly (using new keyword)
By using an Array constructor (using new keyword)
1) JavaScript array literal
The syntax of creating array using array literal is given below:
var arrayname=[value1,value2.....valueN];
As you can see, values are contained inside [ ] and separated by , (comma).
Let's see the simple example of creating and using array in JavaScript.
<script>
var emp=["Sonoo","Vimal","Ratan"];
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br/>");
}
</script>
The .length property returns the length of an array.
Output
Sonoo
Vimal
Ratan
2) JavaScript Array directly (new keyword)
The syntax of creating array directly is given below:
var arrayname=new Array();
Here, new keyword is used to create instance of array.
Let's see the example of creating array directly.
<script>
var i;
var emp = new Array();
emp[0] = "Arun";
emp[1] = "Varun";
emp[2] = "John";
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
Output
Arun
Varun
John
3) JavaScript array constructor (new keyword)
Here, you need to create instance of array by passing arguments in constructor so that we don't
have to provide value explicitly.
The example of creating object by array constructor is given below.
<script>
var emp=new Array("Jai","Vijay","Smith");
for (i=0;i<emp.length;i++){
document.write(emp[i] + "<br>");
}
</script>
Output
Jai
Vijay
Smith
Additional Resources:
https://www.w3schools.com/js/js_arrays.asp
Practice Questions:
1.Define Array.
2.Describe the array in javascript.
3. Write a JavaScript program to find the length of an array.
6.Javascript operations:
What is an Operator?
Let us take a simple expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and ‘+’ is
called the operator. JavaScript supports the following types of operators.
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Arithmetic Operators
JavaScript supports the following arithmetic operators −
Assume variable A holds 10 and variable B holds 20, then −
1 + (Addition)
Adds two operands
Ex: A + B will give 30
2 - (Subtraction)
Subtracts the second operand from the first
Ex: A - B will give -10
3 * (Multiplication)
Multiply both operands
Ex: A * B will give 200
4 / (Division)
Divide the numerator by the denominator
Ex: B / A will give 2
5 % (Modulus)
Outputs the remainder of an integer division
Ex: B % A will give 0
6 ++ (Increment)
Increases an integer value by one
Ex: A++ will give 11
7 -- (Decrement)
Decreases an integer value by one
Ex: A-- will give 9
Note − Addition operator (+) works for Numeric as well as Strings. e.g. "a" + 10 will give "a10".
Example
<html>
<body>
1 = = (Equal)
Checks if the value of two operands are equal or not, if yes, then the
condition becomes true.
Ex: (A == B) is not true.
2 != (Not Equal)
Checks if the value of two operands are equal or not, if the values
are not equal, then the condition becomes true.
Ex: (A != B) is true.
2 || (Logical OR)
If any of the two operands are non-zero, then the condition becomes
true.
Ex: (A || B) is true.
3 ! (Logical NOT)
Reverses the logical state of its operand. If a condition is true, then
the Logical NOT operator will make it false.
Ex: ! (A && B) is false.
Example
<html>
<body>
<script type = "text/javascript">
<!--
var a = true;
var b = false;
var linebreak = "<br />";
document.write("(a && b) => ");
result = (a && b);
document.write(result);
document.write(linebreak);
document.write("(a || b) => ");
result = (a || b);
document.write(result);
document.write(linebreak);
document.write("!(a && b) => ");
result = (!(a && b));
document.write(result);
document.write(linebreak);
//-->
</script>
<p>Set the variables to different values and different operators and then try...</p>
</body> </html>
Output
(a && b) => false
(a || b) => true
!(a && b) => true
Set the variables to different values and different operators and then try...
Bitwise Operators
JavaScript supports the following bitwise operators −
Assume variable A holds 2 and variable B holds 3, then −
2 | (BitWise OR)
It performs a Boolean OR operation on each bit of its integer
arguments.
Ex: (A | B) is 3.
3 ^ (Bitwise XOR)
It performs a Boolean exclusive OR operation on each bit of its
integer arguments. Exclusive OR means that either operand one is
true or operand two is true, but not both.
Ex: (A ^ B) is 1.
4 ~ (Bitwise Not)
It is a unary operator and operates by reversing all the bits in the
operand.
Ex: (~B) is -4.
Assignment Operators
JavaScript supports the following assignment operators −
1 = (Simple Assignment )
Assigns values from the right side operand to the left side operand
Ex: C = A + B will assign the value of A + B into C
Note − Same logic applies to Bitwise operators so they will become like <<=, >>=, >>=, &=, |=
and ^=.
Example
<html>
<body>
<script type = "text/javascript">
<!--
var a = 33;
var b = 10;
var linebreak = "<br />";
document.write("Value of a => (a = b) => ");
result = (a = b);
document.write(result);
document.write(linebreak);
1 ? : (Conditional )
If Condition is true? Then value X : Otherwise value Y
Example
<html>
<body>
<script type = "text/javascript">
<!--
var a = 10;
var b = 20;
var linebreak = "<br />";
document.write ("((a > b) ? 100 : 200) => ");
result = (a > b) ? 100 : 200;
document.write(result);
document.write(linebreak);
document.write ("((a < b) ? 100 : 200) => ");
result = (a < b) ? 100 : 200;
document.write(result);
document.write(linebreak);
//-->
</script>
<p>Set the variables to different values and different operators and then try...</p>
</body>
</html>
Output
((a > b) ? 100 : 200) => 200
((a < b) ? 100 : 200) => 100
Set the variables to different values and different operators and then try...
typeof Operator
The typeof operator is a unary operator that is placed before its single operand, which can be of
any type. Its value is a string indicating the data type of the operand.
The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string,
or boolean value and returns true or false based on the evaluation.
Here is a list of the return values for the typeof Operator.
Number "number"
String "string"
Boolean "boolean"
Object "object"
Function "function"
Undefined "undefined"
Null "object"
Example
<html>
<body>
<script type = "text/javascript">
<!--
var a = 10;
var b = "String";
var linebreak = "<br />";
result = (typeof b == "string" ? "B is String" : "B is Numeric");
document.write("Result => ");
document.write(result);
document.write(linebreak);
result = (typeof a == "string" ? "A is String" : "A is Numeric");
document.write("Result => ");
document.write(result);
document.write(linebreak);
//-->
</script>
<p>Set the variables to different values and different operators and then try...</p>
</body>
</html>
Output
Result => B is String
Result => A is Numeric
Set the variables to different values and different operators and then t
1. Arithmetic Expression
Definition: An arithmetic expression involves arithmetic operators to perform
mathematical operations.
Common Operators: +, -, *, /, %, ** (exponentiation).
let a = 10, b = 5;
// Addition
let sum = a + b; // 15
// Subtraction
let diff = a - b; // 5
// Multiplication
let product = a * b; // 50
// Division
let quotient = a / b; // 2
// Modulus
let remainder = a % b; // 0
Logical Expression
Definition: A logical expression uses logical operators to combine or invert boolean values.
Common Operators:
o && (Logical AND)
o || (Logical OR)
o ! (Logical NOT).
let x = true, y = false;
// Logical AND
let andResult = x && y; // false (both must be true)
// Logical OR
let orResult = x || y; // true (one or both must be true)
// Logical NOT
let notResult = !x; // false (inverts the value)
3. Relational Expression
Definition: A relational expression compares two values and returns a boolean result (true
or false).
Common Operators:
o <, >, <=, >=
o == (equality)
o === (strict equality)
o != (inequality)
o !== (strict inequality).
Example:
javascript
Copy code
let p = 20, q = 15;
// Greater than
let isGreater = p > q; // true
// Less than
let isLess = p < q; // false
// Equality
let isEqual = p == q; // false
// Strict Equality
let isStrictEqual = p === "20"; // false (data type mismatch)
// Not Equal
let isNotEqual = p != q; // true
Additional Resources:
https://www.geeksforgeeks.org/introduction-to-javascript/
Practice Questions:
1. List any four arithmetic operators in JavaScript.
2. Define an expression in JavaScript with an example.
3. What does the === operator check in JavaScript?
4. Write a JavaScript program to evaluate the following expression: ((5 + 10) * 2) / 5.
5. Explain the different kind of operator in javascript.
Decision making in Java script
JavaScript supports conditional statements which are used to perform different actions
based on different conditions. Here we will explain the if..else statement.
The following flow chart shows how the if-else statement works.
JavaScript supports the following forms of if..else statement −
if statement
if...else statement
if...else if... statement.
if statement
The if statement is the fundamental control statement that allows JavaScript to make
decisions and execute statements conditionally.
Syntax
if (expression)
{
Statement(s) to be executed if expression is true
}
Here a JavaScript expression is evaluated. If the resulting value is true, the given statement(s) are
executed. If the expression is false, then no statement would be not executed. Most of the times,
you will use comparison operators while making decisions.
Example
<html>
<body>
<script type = "text/javascript">
<!--
var age = 20;
if( age > 18 ) {
document.write("<b>Qualifies for driving</b>");
}
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Output
Qualifies for driving
Set the variable to different value and then try...
if...else statement
The 'if...else' statement is the next form of control statement that allows JavaScript to
execute statements in a more controlled way.
Syntax
if (expression) {
Statement(s) to be executed if expression is true
}
else {
Statement(s) to be executed if expression is false
}
Here JavaScript expression is evaluated. If the resulting value is true, the given statement(s) in the
‘if’ block, are executed. If the expression is false, then the given statement(s) in the else block are
executed.
Example
<html>
<body>
<script type = "text/javascript">
<!--
var age = 15;
switch statement
Starting with JavaScript 1.2, you can use a switch statement which handles exactly this situation,
and it does so more efficiently than repeated if...else if statements.
Flow Chart
The following flow chart explains a switch-case statement works.
Syntax
The objective of a switch statement is to give an expression to evaluate and several different
statements to execute based on the value of the expression. The interpreter checks
each case against the value of the expression until a match is found. If nothing matches,
a default condition will be used.
switch (expression) {
case condition 1: statement(s)
break;
case condition 2: statement(s)
break;
...
case condition n: statement(s)
break;
default: statement(s)
}
The break statements indicate the end of a particular case. If they were omitted, the interpreter
would continue executing each statement in each of the following cases.
We will explain break statement in Loop Control chapter.
Example
<html>
<body>
<script type = "text/javascript">
<!--
var grade = 'A';
document.write("Entering switch block<br />");
switch (grade) {
case 'A': document.write("Good job<br />");
break;
case 'B': document.write("Pretty good<br />");
break;
case 'C': document.write("Passed<br />");
break;
case 'D': document.write("Not so good<br />");
break;
case 'F': document.write("Failed<br />");
break;
default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Output
Entering switch block
Good job
Exiting switch block
Set the variable to different value and then try...
Break statements play a major role in switch-case statements. Try the following code that uses
switch-case statement without any break statement.
<html>
<body>
<script type = "text/javascript">
<!--
var grade = 'A';
document.write("Entering switch block<br />");
switch (grade) {
case 'A': document.write("Good job<br />");
case 'B': document.write("Pretty good<br />");
case 'C': document.write("Passed<br />");
case 'D': document.write("Not so good<br />");
case 'F': document.write("Failed<br />");
default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Output
Entering switch block
Good job
Pretty good
Passed
Not so good
Failed
Unknown grade
Exiting switch block
Set the variable to different value and then try...
Syntax
The syntax of while loop in JavaScript is as follows −
while (expression)
{
Statement(s) to be executed if expression is true
}
Example
<html>
<body>
<script type = "text/javascript">
<!--
var count = 0;
document.write("Starting Loop ");
while (count < 10) {
document.write("Current Count : " + count + "<br />");
count++;
}
document.write("Loop stopped!");
//-->
</script>
p>Set the variable to different value and then try...</p>
</body>
</html>
Output
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!
Set the variable to different value and then try...
Syntax
The syntax for do-while loop in JavaScript is as follows −
do
{
Statement(s) to be executed;
} while (expression);
Note − Don’t miss the semicolon used at the end of the do...while loop.
Example
<html>
<body>
<script type = "text/javascript">
<!--
var count = 0;
for Loop:
The 'for' loop is the most compact form of looping. It includes the following three important parts
−
The loop initialization where we initialize our counter to a starting value. The initialization
statement is executed before the loop begins.
The test statement which will test if a given condition is true or not. If the condition is true,
then the code given inside the loop will be executed, otherwise the control will come out of
the loop.
The iteration statement where you can increase or decrease your counter.
You can put all the three parts in a single line separated by semicolons.
Flow Chart
The flow chart of a for loop in JavaScript would be as follows −
Syntax
The syntax of for loop is JavaScript is as follows −
for (initialization; test condition; iteration statement)
{
Statement(s) to be executed if test condition is true
}
Example
<html>
<body>
<script type = "text/javascript">
<!--
var count;
document.write("Starting Loop" + "<br />");
for(count = 0; count < 10; count++) {
document.write("Current Count : " + count );
document.write("<br />");
}
document.write("Loop stopped!");
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Output
Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Loop stopped!
Set the variable to different value and then try...
Example
The following example illustrates the use of a break statement with a while loop. Notice how the
loop breaks out early once x reaches 5 and reaches to document.write (..) statement just below to
the closing curly brace −
<html>
<body>
<script type = "text/javascript">
<!--
var x = 1;
document.write("Entering the loop<br /> ");
if (x == 5) {
continue; // skip rest of the loop body
}
document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
//-->
</script>
<p>Set the variable to different value and then try...</p>
</body>
</html>
Output
Entering the loop
2
3
4
6
7
8
9
10
Exiting the loop!
Set the variable to different value and then try...
Additional Resources:
https://www.w3schools.com/js/js_loop_for.asp
Practice Questions:
1.Write the syntax of if..else statement?
2. Write the syntax of for statement?
3.Explain the switch case statement.
4.Explain the syntax of for,while,do-while statement using example.
Calling a Function
To invoke a function somewhere later in the script, you would simply need to write the name of
that function as shown in the following code.
<html>
<head>
<script type = "text/javascript">
function sayHello() {
document.write ("Hello there!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello">
</form>
<p>Use different text in write method and then try...</p>
</body>
</html>
Output
Say Hello
Use different text in write method and then try...
Function Parameters
Till now, we have seen functions without parameters. But there is a facility to pass different
parameters while calling a function. These passed parameters can be captured inside the function
and any manipulation can be done over those parameters. A function can take multiple parameters
separated by comma.
Example
Try the following example. We have modified our sayHello function here. Now it takes two
parameters.
<html>
<head>
<script type = "text/javascript">
function sayHello(name, age) {
document.write (name + " is " + age + " years old.");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello('Zara', 7)" value = "Say Hello">
</form>
<p>Use different parameters inside the function and then try...</p>
</body>
</html>
Output
Say Hello
A JavaScript function can have an optional return statement. This is required if you want
to return a value from a function. This statement should be the last statement in a function.
For example, you can pass two numbers in a function and then you can expect the function to
return their multiplication in your calling program.
Example
Try the following example. It defines a function that takes two parameters and concatenates them
before returning the resultant in the calling program.
<html>
<head>
<script type = "text/javascript">
function concatenate(first, last) {
var full;
full = first + last;
return full;
}
function secondFunction() {
var result;
result = concatenate('Zara', 'Ali');
document.write (result );
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "secondFunction()" value = "Call Function">
</form>
<p>Use different parameters inside the function and then try...</p>
</body>
</html>
Output
Call Function
Additional Resources:
https://www.w3schools.com/js/js_functions.asp
Practice Questions:
1.Define functions.
2.Explain functions in JavaScript.
3.Explain return statement.
9. Dialog Boxes:
JavaScript supports three important types of dialog boxes. These dialog boxes can be used
to raise and alert, or to get confirmation on any input or to have a kind of input from the users.
Here we will discuss each dialog box one by one.
Alert Dialog Box
An alert dialog box is mostly used to give a warning message to the users. For example, if
one input field requires to enter some text but the user does not provide any input, then as a part
of validation, you can use an alert box to give a warning message.
Nonetheless, an alert box can still be used for friendlier messages. Alert box gives only one button
"OK" to select and proceed.
Example
<html>
<head>
<script type = "text/javascript">
<!--
function Warn() {
alert ("This is a warning message!");
document.write ("This is a warning message!");
}
//-->
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "Warn();" />
</form>
</body>
</html>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "getConfirmation();" />
</form>
</body>
</html>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type = "button" value = "Click Me" onclick = "getValue();" />
</form>
</body>
</html>
Additional Resources:
https://www.geeksforgeeks.org/javascript-dialogue-boxes/
Practice Questions:
1.Describe dialog boxes.
2.List out the dialog box supported by JavaScript.
3.Define confirm box.
4.Explain in detail about dialog boxes with example program.
10.Mysql
Introduction to MYSQL:
MySQL is a fast, easy to use relational database. It is currently the most popular open-
source database. It is very commonly used in conjunction with PHP scripts to create powerful and
dynamic server-side applications.
MySQL is used for many small and big businesses. It is developed, marketed and supported
by MySQL AB, a Swedish company. It is written in C and C++.
What is a Database?
A database is a separate application that stores a collection of data. Each database has one
or more distinct APIs for creating, accessing, managing, searching and replicating the data it holds.
Nowadays, we use relational database management systems (RDBMS) to store and manage
huge volume of data. This is called relational database because all the data is stored into different
tables and relations are established using primary keys or other keys known as Foreign Keys.
A Data Type specifies a particular type of data, like integer, floating points, Boolean etc.
It also identifies the possible values for that type, the operations that can be performed on that type
and the way the values of that type are stored.
MySQL supports a lot number of SQL standard data types in various categories. It uses
many different data types broken into mainly three categories: numeric, date and time, and string
types.
Maximum size of
TEXT(size) Where size is the number of characters to store.
65,535 characters.
Where size is the number of binary characters
Maximum size of
BINARY(size) to store. Fixed-length strings. Space padded on
255 characters.
right to equal size characters.
MYSQL Architecture
MySQL architecture consists of five primary subsystems that work together to respond
to a request made to MySQL database server.
Query Engine
SQL Interface: The SQL interface provides the mechanisms to receive commands
and transmit results to the user.
Parser: The parser constructs a query structure used to represent the query
statement in memory as a tree structure.
Query Optimizer: The optimizer used is a SELECT-PROJECT-JOIN strategy that
attempts to restructure the query by first doing any restrictions.
Query Execution: Execution of the query is handled by a set of library methods
designed to implement a particular query.
Query Cache: This enables the system to check for frequently used queries and
shortcut the entire query optimization and execution stages altogether.
Buffer Manager/Cache and Buffers:
The caching and buffers subsystem is responsible for ensuring that the most frequently
used data are available in the most efficient manner possible.
The storage manager interfaces with the operating system to write data to the disk
efficiently.
The storage manager writes to disk all of the data in the user tables, indexes, and
logs as well as the internal system data.
The Transaction Manager
The function of the Transaction manager is to facilitate concurrency in data access.
This subsystem provides a locking facility to ensure that multiple simultaneous
users access the data in consistent way.
Recovery Manager.
The Recovery Manager’s job is to keep copies of data for retrieval later, in case of
loss of data.
It also logs commands that modify the data and other significant events inside the
database
Create Database:
To create the new database, check the current databases to make sure a database of that
name doesn’t already exist;
Then create the new one, and verify the existence of the new database:
Syntax:
mysql> CREATE DATABASE
Eg:
mysql> CREATE DATABASE Student;
SELECT Database
SELECT Database is used in MySQL to select a particular database to work with. This query
is used when multiple databases are available with MySQL Server.
We can use SQL command USE to select a particular database.
Syntax:
USE database_name;
Example:
USE customers;
Drop Database
We can drop/delete/remove a MySQL database easily with the MySQL command. You should
be careful while deleting any database because you will lose your all the data available in your
database.
Syntax:
DROP DATABASE database_name;
Example:
DROP DATABASE employees;
4.6 MYSQLTables:
A table is a collection of rows, each row holding data for one record, each record
containing chunks of information called fields.
CREATE Table
CREATE TABLE command is used to create a new table into the database. A table
creation command requires three things:
o Name of the table
o Names of fields
o Definitions for each field
Syntax:
CREATE TABLE table_name (column_name column_type...);
Example:
CREATE TABLE cus_tbl(
cus_id INT NOT NULL AUTO_INCREMENT,
cus_firstname VARCHAR(100) NOT NULL,
cus_surname VARCHAR(100) NOT NULL,
PRIMARY KEY ( cus_id ) );
ALTER Table
MySQL ALTER statement is used when you want to change the name of your table or any
table field. It is also used to add or delete an existing column in a table.
The ALTER statement is always used with "ADD", "DROP" and "MODIFY" commands
according to the situation.
1) Add a column in the table
Syntax:
ALTER TABLE table_name ADD new_column_name column_def
[ FIRST | AFTER column_name ];
Example:
ALTER TABLE cus_tbl ADD cus_age varchar(40) NOT NULL;
2) MODIFY column in the table
The MODIFY command is used to change the column definition of the table.
Syntax:
ALTER TABLE table_name MODIFY column_name column_def
[ FIRST | AFTER column_name ];
Example:
ALTER TABLE cus_tbl MODIFY cus_name varchar(50) NULL;
3) DROP column in table
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
Example:
ALTER TABLE cus_tbl DROP COLUMN cus_address;
4) RENAME column in table
Syntax:
ALTER TABLE tbl_name CHANGE COLUMN ol_name ne_name
Example:
ALTER TABLE cus_tbl CHANGE COLUMN cus_name cus_title
varchar(20) NOT NULL;
RENAME table
Syntax:
ALTER TABLE table_name RENAME TO new_table_name;
Example:
ALTER TABLE cus_tbl RENAME TO cus_table;
TRUNCATE Table
MYSQL TRUNCATE statement removes the complete data without removing its
structure.
The TRUNCATE TABLE statement is used when you want to delete the complete data
from a table without removing the table structure.
Syntax: TRUNCATE TABLE table_name;
Example: TRUNCATE TABLE cus_tbl;
DESCRIBE Table
It provides a description of the specified table or view.
For a list of tables in the current schema, use the Show Tables command.
For a list of views in the current schema, use the Show Views command.
For a list of available schemas, use the Show Schemas command.
If the table or view is in a particular schema, qualify it with the schema name.
If the table or view name is case-sensitive, enclose it in single quotes.
It displays the entire column from all the tables and views in a single schema by using
character '*'.
Syntax: DESCRIBE { table-Name | view-Name };
Eg: MYSQL> DESCRIBE age information;
MYSQL Statements
A list of commonly used MySQL statements to insert record, update record, delete record,
select record are given below.
Select Statement
The MYSQL SELECT statement is used to fetch data from the one or more tables. We can
retrieve records of all fields or specified fields.
Syntax: (Particular field)
SELECT expressions
FROM tables
[WHERE conditions];
Syntax: ( for all fields)
SELECT * FROM tables [WHERE conditions];
Example:
SELECT officer_name, address FROM officers
SELECT * FROM officers
MYSQL SELECT statement can also be used to retrieve records from multiple tables by
using JOIN statement.
Example
SELECT officers.officer_id, students.student_name
FROM students
INNER JOIN officers
ON students.student_id = officers.officer_id ORDER BY student_id;
Insert Statement
MYSQL INSERT statement is used to insert data in MYSQL table within the database.
We can insert single or multiple records using a single query in MYSQL.
Syntax:
INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );
Note: Field name is optional. If you want to specify partial values, field name is mandatory.
Example:
INSERT INTO emp VALUES (7, 'Sonoo', 40000);
(Or)
INSERT INTO emp(id,name,salary) VALUES (7, 'Sonoo', 40000);
Update Statement
MYSQL UPDATE statement is used to update data of the MYSQL table within the
database. It is used when you need to modify the table.
Syntax:
UPDATE table_name
SET field1=new-value1, field2=new-value2
[WHERE Clause]
Note:
One or more field can be updated altogether.
Any condition can be specified by using WHERE clause.
You can update values in a single table at a time.
WHERE clause is used to update selected rows in a table.
Example:
This query will update cus_surname field for a record having cus_id as 5.
UPDATE cus_tbl
SET cus_surname = 'Ambani'
WHERE cus_id = 5;
Delete Statement
MySQL DELETE statement is used to delete data from the MySQL table within the
database. By using delete statement, we can delete records on the basis of conditions.
Syntax:
DELETE FROM table_name
WHERE
(Condition specified);
Example:
DELETE FROM cus_tbl
WHERE cus_id = 6;
MYSQL JOINS
MYSQL JOINS are used with SELECT statement.
It is used to retrieve data from multiple tables.
It is performed whenever you need to fetch records from two or more tables.
There are three types of MYSQL joins:
o MYSQL INNER JOIN (or sometimes called simple join)
o MYSQL LEFT OUTER JOIN (or sometimes called LEFT JOIN)
o MYSQL RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
MYSQL Inner JOIN (Simple Join)
The MYSQL INNER JOIN is used to return all rows from multiple tables where the join
condition is satisfied. It is the most common type of join.
Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Image representation:
Example:
SELECT officers.officer_name, officers.address, students.course_name
FROM officers
INNER JOIN students
ON officers.officer_id = students.student_id;
MYSQL Left Outer Join:
The LEFT OUTER JOIN returns all rows from the left hand table specified in the ON
condition and only those rows from the other table where the join condition is fulfilled.
Syntax:
SELECT columns FROM table1 LEFT [OUTER] JOIN table2 ON table1.column = table2.colu
mn;
Image representation:
Example:
SELECT officers.officer_name, officers.address, students.course_name
FROM officers
LEFT JOIN students
ON officers.officer_id = students.student_id;
MYSQL Right Outer Join:
The MYSQL Right Outer Join returns all rows from the RIGHT-hand table specified in the
ON condition and only those rows from the other table where the join condition is fulfilled.
Syntax:
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;
Image representation:
Example:
SELECT officers.officer_name, officers.address, students.course_name, students.student
_name
FROM officers
RIGHT JOIN students
ON officers.officer_id = students.student_id;
Additional Resources:
https://www.w3schools.com/sql/sql_select.asp
Practice Questions:
1.What is SQL?
2.What are table creation command?
3.What is the purpose of the USE command?
4.Write a syntax MYSQL select query?
5.What are the administrative MYSQL command?
6.Explain the create database and tables.
7.Explain the select,Insert,update and delete statement?
8.Explain the table joins using MYSQL?
9.Write short note on USE command.
10.Explain the basics of MYSQL?