Uploaded by tanjinanajnin

SQL

advertisement
What is SQL?
Businesses and other organizations use SQL programs to access and manipulate the
information and data in their databases and create and alter new tables. According to
Microsoft, a database is a tool for collecting and organizing information. Databases can
store information about people, products, orders or anything else. Many databases start
in a word processing program or spreadsheet. As they get larger, many businesses find
it helpful to transfer them to a database created by a database management system.
Why to use SQL?
SQL helps control information stored in databases, allowing users to retrieve the
specific data they’re looking for when they need it.
While it’s a simple programming language, SQL is very powerful. In fact, SQL can insert
data into database tables, modify data in existing database tables and delete data from
SQL database tables. In addition, SQL can modify the database structure itself by
creating, modifying, and deleting tables and other database objects.
SQL uses a set of commands to manipulate the data in databases. Examples include
SQL INSERT, which is used to add data to database tables; SQL SELECT, which
retrieves data from database tables; and SQL UPDATE, which modifies existing
database records.
SQL experience is one of the most in-demand career skills.
What are the Advantages of SQL?
•
•
•
•
•
•
•
•
•
•
Standardized Language.
Reduced training costs
Faster and Efficient Query Processing. SQL works with an efficient speed.
No need for large and complex code lines for data extraction.
Application longevity
Portable.
Internet Usage.
Reduced dependence on a single vendor.
Large User Community.
.
Data Type
Installation Process
Create Database
Connect to database
Examples:
 Create table
CREATE TABLE Customer_T(
CustomerID NUMBER(11,0) NOT NULL PRIMARY KEY,
CustomerName VARCHAR2(25) NOT NULL,
CustomerAddress VARCHAR2(30),
CustomerCity VARCHAR2(20),
CustomerState CHAR(2),
CustomerPostalCode VARCHAR2(9),
CONSTRAINT Customer_PK PRIMARY KEY (CustomerID)
);

Create Table with Reference
CREATE TABLE Order_T (
OrderID NUMBER(11,0) NOT NULL PRIMARY KEY,
OrderDate DATE DEFAULT SYSDATE,
CustomerID NUMBER(11,0),
CONSTRAINT Order_FK FOREIGN KEY (CustomerID)
REFERENCES Customer_T(CustomerID)
);
 Insert Data into table
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (1, 'Contemporary Casuals', '1355 S Hines Blvd', 'Gainesville', 'FL', '326012871');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (2, 'Value Furniture', '15145 S.W. 17th St.', 'Plano', 'TX', '75094-7743');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (3, 'Home Furnishings', '1900 Allard Ave.', 'Albany', 'NY', '12209-1125');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (4, 'Eastern Furniture', '1925 Beltline Rd.', 'Carteret', 'NJ', '07008-3188');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (5, 'Impressions', '5585 Westcott Ct.', 'Sacramento', 'CA', '94206-4056');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (6, 'Furniture Gallery', '325 Flatiron Dr.', 'Boulder', 'CO', '80514-4432');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (7, 'Period Furniture', '394 Rainbow Dr.', 'Seattle', 'WA', '97954-5589');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (8, 'California Classics', '816 Peach Rd.', 'Santa Clara', 'CA', '96915-7754');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (9, 'M and H Casual Furniture', '3709 First Street', 'Clearwater', 'FL', '346202314');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (10, 'Seminole Interiors', '2400 Rocky Point Dr.', 'Seminole', 'FL', '346464423');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (11, 'American Euro Lifestyles', '2424 Missouri Ave N.', 'Prospect Park', 'NJ',
'07508-5621');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (12, 'Battle Creek Furniture', '345 Capitol Ave. SW', 'Battle Creek', 'MI', '490153401');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (13, 'Heritage Furnishings', '66789 College Ave.', 'Carlisle', 'PA', '17013-8834');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (14, 'Kaneohe Homes', '112 Kiowai St.', 'Kaneohe', 'HI', '96744-2537');
INSERT INTO Customer_T (CustomerID, CustomerName, CustomerAddress,
CustomerCity, CustomerState, CustomerPostalCode)
VALUES (15, 'Mountain Scenes', '4132 Main Street', 'Ogden', 'UT', '84403-4432');







What is Customer Name and Customer ID?
View Customer name and address who have Customer id of 8?
What is the sum and average amount of order amount?
Show the maximum sales amount of vehicles.
What is the total records of Customer_ID from Customer table?
Show the Lowest sales order amount of vehicles.
Remove Table
DROP TABLE Customer_T;
 Delete Data
DELETE FROM Customer_T;
 Update Data (customer)
UPDATE Product_T
SET ProductStandardPrice = 775
WHERE ProductID = 7;
Download