Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join...

11
Introduction to Query Language and SQL

Transcript of Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join...

Page 1: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

Introduction to Query Language and SQL

Page 2: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

Basic Query Language Operators

• Selection

• Projection

• Join

• Aggregates– Sum, Count, Max, Min, Avg

• SubTotal

• Calculated field

Page 3: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

SQL Insert CommandINSERT INTO tableName VALUES (field values separated by commas);

INSERT INTO tableName (Column names separated by commas)VALUES (field values separated by commas);

Ex 1. Customer table with CID, CNAME, CITY, RATING.

a. INSERT INTO CUSTOMER VALUES (‘C1’, ‘SMITH’, ‘SF’, ‘A’);

b. INSERT INTO CUSTOMER (CID, CNAME,RATING) VALUES (‘C1’, ‘SMITH’, ‘A’);

Ex 2. Orders table with OID, OrderDate, CID, SalesPerson

INSERT INTO ORDERS VALUES (‘O11’, #9/28/02#, ‘C1’, ‘Peter’);

Page 4: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

Creating A String Containing SQL Insert Command

Assuming the four fields of the new customer record are entered in textboxes:

Dim strSQLInsert As String

strSQLInsert = "Insert into Customer values ('"

strSQLInsert = strSQLInsert & TextBox1.Text & "','" & TextBox2.Text & "','"

strSQLInsert = strSQLInsert & TextBox3.Text & "','" & TextBox4.Text & "')"

Page 5: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

SQL Delete Command

DELETE FROM tableName WHERE criteria;

Ex 1. Delete a record from the Customer table.

DELETE FROM CUSTOMER WHERE CID = ‘C1’;

Page 6: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

Creating A String Containing SQL Delete Command

Assuming the deleted record’s ID is selected from a list box:

Dim strSQLDel As String = “DELETE FROM CUSTOMER WHERE CID = ‘“

strSQLDel = strSQLDel & ListBox1.SelectedItem & "'"

Page 7: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

SQL Update Command

UPDATE tableName SET field = new value WHERE criteria;

Ex.

1. UPDATE CUSTOMER SET RATING = ‘A’ WHERE CID=‘C1’;

2. UPDATE EMPLOYEE SET SALARY = SALARY*1.05

Page 8: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

Creating A String Containing SQL Update Command

Assuming the CID is selected from a list box, and the new rating is entered in a text box:

Dim strSQLUpd As String = "Update customer set rating = '" & textbox1.text & "'"

strSQLUpd = strSQLUpd & " where cid='" & ListBox1.SelectedItem & "'"

Page 9: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

SQL Select Command

1. SELECT * FROM tableName WHERE criteria;

Ex. SELECT * FROM CUSTOMER WHERE RATING = ‘a’;

2. SELECT fields FROM tableName WHERE criteria;

Ex. SELECT CID, CNAME, RATING FROM EMPLOYEE

WHERE CITY = ‘sf’;

Page 10: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

Aggregates1. SELECT COUNT(CID) AS custCount FROM

CUSTOMER;

2. SELECT COUNT(EID) AS empCount, SUM(SALARY) AS totalSalary, AVG(SALARY) AS avgSalary FROM EMPLOYEE;

3. Group By:

Ex. SELECT CITY, COUNT(CID) As CustCount FROM

CUSTOMER GROUP BY CITY;

Ex. SELECT SEX, AVG(SALARY) AS avgSalary FROM

EMPLOYEE GROUP BY SEX;

Note: Alias CustCount

Page 11: Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.

Creating A String Containing SQL Select Command

•Assuming the rating is selected from a list box:

•dim strSQL as string = "select * from customer where rating = ‘“•strSQL = strSQL & ListBox1.SelectedItem & "‘”