Saturday, 4 May 2013

SQL CREATE TABLE



Purpose:
This command is use for create a table file

Syntax:
CREATE TABLE table-name

Complete Syntax:

CREATE TABLE file
(column (attribute) name1 data types  [PRIMARY KEY] or [FOREIGN KEY references column-name]

column (attribute) name2 data types,
.........,
……..
)
Note:  Primary/Foreign key is optional. Only apply if column is set as primary /Foreign key.

Examples:
With PRIMARY KEY

CREATE TABLE student
(
sid char(5) PRIMARY KEY,
age int,
fname char(20),
lname char(20)
)

With FOREIGN KEY

CREATE TABLE register
(
sid char(12) FOREIGN KEY references student(sid),
cid char(6) FOREIGN KEY references course(cid),
PRIMARY KEY (sid,cid)  or  CONSTRAINT PK_anyname PRIMARY KEY (sid,cid)
)

SQL USE





Purpose: 

This command is use to open or activate a database file





Syntax:
   

USE data database-file-name




Examples:

USE student

 
USE employee

SQL CREATE DATABASE



Purpose: 
 
This command is use for create a database file

Syntax:

CREATE DATABASE database-file-name

Examples:

 CREATE DATABASE student

CREATE DATABASE employee

Friday, 3 May 2013

SQL Select



SELECT
This command is used to retrieved or accessed the desired records from table(s). This is very important command and mostly used.
Syntax: 
SELECT column1,column2,... FROM table-name WHERE conditions [operators]
 Examples:
 SELECT *FROM employee 
This command show all records of table employee. Use *before FROM mean all records. And there is no special criteria for filtering records. If you want to select the total salary paid for its employees more than 20000, the SQL query would be like:
SELECT *FROM employee WHERE salary > 20000
If salary paid for its employees more than 20000, and less than 30000 the SQL query would be like:
SELECT *FROM employee WHERE salary > 20000 AND salary < 30000

If you want the followng result then apply command as
Full Name
----------------
Abdul Hamid
Ghulam Hussain 
SELECT first_name+Last_name as "Full Name" FROM student

The following command shows top 20 percent of recrod from the table student

SELECT TOP 20 PERCENT *FROM  student
The following command shows the Reg_No of those students who have age between 20 to 40

SELECT Reg_no FROM student WHERE age >= 20 and age <= 40