Tuesday, May 31, 2016

WDP - Database Queries

SELECT

The SELECT statement is used to select data from a database.

SYNTAX

SELECT column_name,column_name
FROM table_name;

        and

SELECT * FROM table_name;

SAMPLE CODE

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
SELECT CustomerName, City FROM Customers;


Output:
CustomerNameCity
Alfreds FutterkisteHamburg
Ana Trujillo Emparedados y heladosMéxico D.F.



JOIN


SQL joins are used to combine rows from two or more tables.


SYNTAX

INNER JOIN table_name

SAMPLE CODE

Customer Table
CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico


Order Table
OrderIDCustomerIDEmployeeIDOrderDateShipperID
10308271996-09-183
103093731996-09-191

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID


Output:

CustomerNameOrderID
Ana Trujillo Emparedados y helados10308
Antonio Moreno Taquería10365



INSERT

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


SYNTAX
INSERT INTO table_name
VALUES (value1,value2,value3,...);   AND

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);


SAMPLE CODE

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
87Wartian HerkkuPirkko KoskitaloTorikatu 38Oulu90110Finland
88Wellington ImportadoraPaula ParenteRua do Mercado, 12Resende08737-363Brazil

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate

FROM Orders

INNER JOIN Customers

ON Orders.CustomerID=Customers.CustomerID;


Output:
OrderIDCustomerNameOrderDate
10248Wilman Kala1996-07-04
10249Tradição Hipermercados1996-07-05



UPDATE

The UPDATE statement is used to update records in a table


SYNTAX

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;


SAMPLE CODE

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico


UPDATE Customers

SET ContactName='Alfred Schmidt', City='Hamburg'

WHERE CustomerName='Alfreds Futterkiste';


Output:
CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteAlfred SchmidtObere Str. 57Hamburg12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico


DELETE

The DELETE statement is used to delete records in a table.


SYNTAX

DELETE FROM table_name
WHERE some_column=some_value;


SAMPLE CODE

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';

Output:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico


DROP

Indexes, tables, and databases can easily be deleted/removed with the DROP statement.


SYNTAX

DROP TABLE table_name

and

DROP INDEX table_name.index_name


CREATE TABLE

The CREATE TABLE statement is used to create a table in a database.


SYNTAX
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);


SAMPLE CODE
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);



Output:
PersonIDLastNameFirstNameAddressCity


Ref:

No comments:

Post a Comment