SQL
Introduction:
SQL is a standard language for accessing and manipulating databases.
What is SQL?• SQL stands for Structured Query Language
• SQL lets you access and manipulate databases
• SQL is an ANSI (American National Standards Institute) standard
What Can SQL do?• 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
SQL DML and DDL
SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).
The query and update commands form the DML part of SQL:
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
The DDL part of SQL permits database tables to be created or deleted. It also define indexes (keys), specify links between tables, and impose constraints between tables. The most important DDL statements in SQL are:
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index
Basic SQL Queries:
//Creating a New Database
Create database mysampledb
Use mysampledb
//Creating New Table
A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.
Create table customers
(
C_Id int NOT NULL primary key,
LastName varchar(255) NOT NULL,
First Name varchar(255),
Address varchar(255),
City varchar(255)
)
//Insert Query
insert into customers values(1,'Aravind','kumer','Anna Nagar 12','Chennai')
insert into customers values(2,'Sundar','Rajan','Villivakkam 13','Chennai')
insert into customers values(3,'Hari','Ram','Perungudi 7','Chennai')
//select Query
select *from customers
select C_Id from customers
select LastName ,FirstName from customers
select Address from customers where C_Id=2
select *from customers where City='chennai'
//update query
update customers set City='chinnur' where C_Id=1
//Alter Column Name
alter table customers Address to Addr varchar(255)
sp_rename 'customers.Address' ,Addr
//Alter Table
//Rename table
sp_rename 'customers details' ,'customersdetails '
//Rename Column
sp_rename 'customers.Address' ,Addr
select *from customersdetails
//Add &drop Column
alter table customersdetails add dob datetime
alter table customersdetails drop column dob
//Distinct
select distinct City from customersdetails
//AND & OR Operators
select Addr from customersdetails where LastName='Hari' and FirstName='Ram'
select Addr ,City from customersdetails where C_Id=4 or LastName='Sundar'
//Order By
select *from customersdetails order by City
Saturday, April 25, 2009
SQL Queries
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment