Watch & Subscribe my SQL videos on YouTube | Join me on Facebook

An introduction to SQL and its Components

February 3, 2009 Leave a comment

–>Introduction:

SQL or Structured Query Language is a language that provides an interface to relational database systems. The proper pronunciation of SQL is “ess cue ell,” and not “sequel” as is commonly heard. SQL was developed by IBM in the 1970s for use in System R, and is a de facto standard, as well as an ISO and ANSI standard. In common usage SQL also encompasses DML (Data Manipulation Language), for INSERTs, UPDATEs, DELETEs and DDL (Data Definition Language), used for creating and modifying tables and other database structures. The development of SQL is governed by standards. A major revision to the SQL standard was completed in 1992, called SQL2. SQL3 support object extensions and are (partially?) implemented in Oracle8 and 9i.

SQL (Structured Query Language) is a database computer language designed for managing data in relational database management systems (RDBMS). Its scope includes data query and update, schema creation and modification, and data access control. SQL was one of the first languages for Edgar F. Codd’s relational model in his influential 1970 paper, “A Relational Model of Data for Large Shared Data Banks” and became the most widely used language for relational databases.
 

–>Language elements:

The SQL language is sub-divided into several language elements, including:

1. Clauses which are in some cases optional, constituent components of statements and queries.

2. Expressions which can produce either scalar values or tables consisting of columns and rows of data.

3. Predicates which specify conditions that can be evaluated to SQL three-valued logic (3VL) Boolean truth values and which are used to limit the effects of statements and queries, or to change program flow.

4. Queries which retrieve data based on specific criteria.

5. Statements which may have a persistent effect on schemas and data, or which may control transactions, program flow, connections, sessions, or diagnostics.

6. SQL statements also include the semicolon (“;”) statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar.

7. Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.
 

–> Queries:

The most common operation in SQL is the query, which is performed with the declarative SELECT statement. SELECT retrieves data from one or more tables, or expressions. Standard SQL statements have no persistent effects on the database. Some non-standard implementations of SELECT can have persistent effects, such as the SELECT INTO syntax that exists in some databases.

Queries allow the user to describe desired data, leaving the database management system (DBMS) responsible for planning, optimizing, and performing the physical operations necessary to produce that result as it chooses.

A Query includes a list of columns to be included in the final result immediately following the SELECT keyword. An asterisk (“*”) can also be used to specify that the query should return all columns of the queried tables. SELECT is the most complex statement in SQL, with optional keywords and clauses that includes:

1. The FROM clause which indicates the table(s) from which data is to be retrieved. The FROM clause can include optional JOIN subclauses to specify the rules for joining tables.

2. The WHERE clause includes a comparison predicate, which restricts the rows returned by the query. The WHERE clause eliminates all rows from the result set for which the comparison predicate does not evaluate to True.

3. The GROUP BY clause is used to project rows having common values into a smaller set of rows. GROUP BY is often used in conjunction with SQL aggregation functions or to eliminate duplicate rows from a result set. The WHERE clause is applied before the GROUP BY clause.

4. The HAVING clause includes a predicate used to filter rows resulting from the GROUP BY clause. Because it acts on the results of the GROUP BY clause, aggregation functions can be used in the HAVING clause predicate.

5. The ORDER BY clause identifies which columns are used to sort the resulting data, and in which direction they should be sorted (options are ascending or descending). Without an ORDER BY clause, the order of rows returned by an SQL query is undefined.
 

–> A simple SELECT example:

SELECT *
FROM dbo.Book
WHERE price > 100.00
ORDER BY title;

 

–> With SQL you can:

– Create new Databases.
– Create new Tables in a database.
– Execute Queries against a database.
– Insert records in a database.
– Update records in a database.
– Delete records from a database.
– Retrieve data from a database.
– Create stored procedures in a database.
– Create views in a database.
– Set permissions on tables, procedures, and views.
 

For the complete history check my following [blog post].


Java Program to write an Applet with menu bar containing Shape, Color item and Sub menu – Q56

January 28, 2009 Leave a comment

Q56: Java Program to write an applet, which has a menu bar containing Shape, Color item. The Shape menu contains sub-menu containing Line, rectangle, Circle, Ellipse & the Color menu contains sub-menu Red, Green, Blue, Pink. On sellecting the appropriate menu item by the user it should be reflected to the Applet on the screen.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

class MenuFrame extends Frame implements ActionListener {
	String col;
    	int shape;

	public MenuFrame(String title) {
		super(title);
      		MenuBar mbar=new MenuBar();
		setMenuBar(mbar);
		
      	Menu shape=new Menu("shape");
		MenuItem item1,item2,item3,item4;
		shape.add(item1 =new MenuItem("Line"));
		shape.add(item2 =new MenuItem("Rectangle"));
	 	shape.add(item3 =new MenuItem("Circle"));
		shape.add(item4 =new MenuItem("Ellipse"));
		mbar.add(shape);
		
		Menu color=new Menu("color");
		MenuItem item5,item6,item7,item8;
		color.add(item5= new MenuItem("red"));
		color.add(item6= new MenuItem("green"));
		color.add(item7= new MenuItem("blue"));
		color.add(item8= new MenuItem("pink"));
		mbar.add(color);

		item1.addActionListener(this);
        	item2.addActionListener(this);
        	item3.addActionListener(this);
        	item4.addActionListener(this);
        	item5.addActionListener(this);
        	item6.addActionListener(this);
        	item7.addActionListener(this);
        	item8.addActionListener(this);
	}
	
	public void paint(Graphics g){
		if (col.equals("red")){
			g.setColor(Color.red);
		}
		else if(col.equals("green")){
			g.setColor(Color.green);
		}
		else if(col.equals("blue")){
			g.setColor(Color.blue);
		}
		else if(col.equals("pink")){
        		g.setColor(Color.pink);
		}
			
		switch(shape){
			case 1:
				g.drawLine(100,100,200,200);	    
				break;
			case 2 :
				g.drawRect(10,10,60,50);
				break;
			case 3:
				g.drawOval(10,10,100,100);
				break;		
			case 4:
				g.drawOval(10,10,100,50);
				break;		
		}
	}
	
	public void actionPerformed(ActionEvent ae){
		String msg="You selected";
		String arg=(String)ae.getActionCommand();
		
		if(arg.equals("red")){
	 			col="red";
	     	}
    	    	else if(arg.equals("blue")){
	 		col="blue";
	     	}
        	else if(arg.equals("green")){
			col="green";
        	}
        	else if (arg.equals("pink")){
	 		col="pink";
        	}         
		
		repaint();

        	if(arg.equals("Line"))
        		shape=1;
        	else if(arg.equals("Rectangle"))
        		shape=2;
        	else if(arg.equals("Circle"))
        		shape=3;
        	else if(arg.equals("Ellipse"))
        		shape=4; 
		
		repaint();
		}
	}
	
public class menushape extends Applet  {
	MenuFrame mf;
	
	public void init() {
    		MenuFrame mf=new MenuFrame("my frame");
     		mf.setVisible(true);
     		mf.setSize(200,200);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to write an Applet with Check Boxes handling and display output – Q55

January 28, 2009 Leave a comment

Q55: Java Program to write an applet, which shows a two set of check boxes. The first set is independent and second set belongs to a checkbox group (like radio buttons). Whichever check box is selected by the user, it should be displayed on the screen.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

abstract class Ap_CheckboxGroupDemo extends Applet implements ItemListener{
 	String msg = "";
 	Checkbox Win98, WinNT;
 	CheckboxGroup cbg;
 	Button Win;
 	MyCheckbox myCheckbox1,myCheckbox2,myCheckbox3;

 	public void init(){
 		Win98 = new Checkbox("Windows 98/Xp");
 		WinNT = new Checkbox("Windows NT/2000");
 		
 		add(Win98);
 		add(WinNT);
 		
 		Win98.addItemListener(this);
 		WinNT.addItemListener(this);
 		
 		cbg = new CheckboxGroup();
 		myCheckbox1 = new MyCheckbox("Item 1",cbg,true);
 		add(myCheckbox1);
 		
 		myCheckbox2 = new MyCheckbox("Item 2",cbg,false);
 		add(myCheckbox2);
 		
 		myCheckbox3 = new MyCheckbox("Item 3",cbg,false);
 		add(myCheckbox3);
	}
	
	public void paint(Graphics g){
		msg = "current state: ";
 		g.drawString(msg,6,80);
 		msg = "Windows 98/Xp:" + Win98.getState();
 		g.drawString(msg,6,100);
 		msg = "Windows NT/2000:" + WinNT.getState();
 	}
 }
 		
 


class MyCheckbox extends Checkbox{
 	
 	public MyCheckbox(String label,CheckboxGroup cbg,boolean flag){
 		super(label,cbg,flag);
 		enableEvents(AWTEvent.ITEM_EVENT_MASK);
	}
	
	protected void processItemEvent(ItemEvent ie){
		showStatus("checkbox name/state: " + getLabel() + 
 					"/" + getState());
 		super.processItemEvent(ie);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to write an Applet which handles user input and show output – Q54

January 27, 2009 Leave a comment

Q54: Java Program to write an applet, which shows one choice item and one list item. Add some items in both. Whenever user selects an item that message should be displayed on the screen.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Ap_ListDemo extends Applet implements ActionListener {
	
	List os, browser;
	String msg = "";
	
	public void init(){
		os = new List(4,true);
		
		browser = new List(4,false);
		
		os.add("Windows 98/Xp");
		os.add("Solaris");
		os.add("Macos");
		
		browser.add("netscape 3.x");
		browser.add("netscape 4.x");
		browser.add("netscape 5.x");
		browser.add("netscape 6.x");
		browser.add("internet explorer 4.0");
		browser.add("internet explorer 5.0");
		browser.add("internet explorer 6.0");
		browser.add("Lynx 2.4");
		
		browser.select(1);
		
		add(os);
		add(browser);
		
		os.addActionListener(this);
		browser.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent ae){
		repaint();
	}
	
	public void paint(Graphics g){
		int idx[];
		
		msg = "current OS";
		idx = os.getSelectedIndexes();
		for(int i = 0; i < idx.length; i++)
			msg += os.getItem(idx[i]) + " ";
		
		g.drawString(msg,6,120);
		msg = "current Browser: ";
		msg += browser.getSelectedItem();
		g.drawString(msg,6,140);
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags:

Java Program to write an Applet having data entry operation (basic calculator) – Q53

January 27, 2009 Leave a comment

Q53: Java Program to write an applet which have three text fields, first two accepts input from user and third one shows the result. It should have four buttons indicating operation add, subtract, multiply and divide, pressing the button should put the result in the third text field.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.awt.TextField.*;

public class Ap_Calc extends Applet implements ActionListener{
	TextField one, two, res;
	Button add, sub, mul, div;

	public void init(){
		Label l1 = new Label("First Number:   ",Label.RIGHT);
		Label l2 = new Label("Second Number:  ",Label.RIGHT);
		Label l3 = new Label("Result:         ",Label.RIGHT);
 
		one = new TextField(5);
		two = new TextField(5);
		res = new TextField(7);

		add = new Button("Add");
		sub = new Button("Substract");
		mul = new Button("Multiply");
		div = new Button("Divide");

		add(l1);
		add(one);
		add(l2);
		add(two);
		add(l3);
		add(res);
	
		add(add);
		add(sub);
		add(div);
		add(mul);

		one.addActionListener(this);
		two.addActionListener(this);
		res.addActionListener(this);
		add.addActionListener(this);
		sub.addActionListener(this);
		div.addActionListener(this);
		mul.addActionListener(this);		
	}

	public void actionPerformed(ActionEvent ae){
		String str = ae.getActionCommand();
		float b1, b2, b3 = 0;
		String msg;
		
		b1 = Float.parseFloat(one.getText());
		b2 = Float.parseFloat(two.getText());
		
		if(str.equals("Add"))
			b3 = b1 + b2;
		if(str.equals("Substract"))
			b3 = b1 - b2;
		if(str.equals("Divide"))
			b3 = b1 / b2;
		if(str.equals("Multiply"))
			b3 = b1 * b2;
		msg = String.valueOf(b3);
		res.setText(msg);	
		repaint();
	}
	
	public void paint(Graphics g){
	}
}

… from College notes (BCA/MCA assignments):


Categories: Java Tags: