7
Database Programming With Visual Basic 6 Introduction to SQL 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 Keep in mind SQL is not case sensitive What Can SQL do? SQL can execute queries against a database SQL can retrieve data from a database SQL can insert, update and delete records in a database SQL can create new databases SQL can create new tables in a database SQL DML and DDL Statements 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 The SQL SELECT Statement The SELECT statement is used to select data from a database.The result is stored in a result table, called the result-set. SQL SELECT Syntax SELECT * or column_name(s) FROM table_name SQL DISTINCT Clause The DISTINCT keyword can be used to elimate the duplicate values. An SQL SELECT Example 1. SELECT * FROM Employee (select all column from table) 2. SELECT Emp_Code,Emp_Name FROM Employee 3. SELECT Distinct Emp_Name FROM Employee SQL WHERE Clause The WHERE clause is used to filter records.

Microsoft Visual Basic 6 (Database Programming SQL)

Embed Size (px)

DESCRIPTION

[email protected]

Citation preview

Page 1: Microsoft Visual Basic 6 (Database Programming SQL)

Database Programming With Visual Basic 6Introduction to SQLSQL 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 Keep in mind SQL is not case sensitive

What Can SQL do? SQL can execute queries against a database SQL can retrieve data from a database SQL can insert, update and delete records in a database SQL can create new databases SQL can create new tables in a database

SQL DML and DDL StatementsSQL 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

The SQL SELECT StatementThe SELECT statement is used to select data from a database.The result is stored in a result table, called the result-set.SQL SELECT Syntax

SELECT * or column_name(s) FROM table_nameSQL DISTINCT Clause

The DISTINCT keyword can be used to elimate the duplicate values.An SQL SELECT Example

1. SELECT * FROM Employee (select all column from table)2. SELECT Emp_Code,Emp_Name FROM Employee3. SELECT Distinct Emp_Name FROM Employee

SQL WHERE ClauseThe WHERE clause is used to filter records.

Page 2: Microsoft Visual Basic 6 (Database Programming SQL)

Database Programming With Visual Basic 6SQL WHERE Syntax

SELECT column_name(s)FROM table_nameWHERE column_name operator value

Operators Allowed in the WHERE ClauseWith the WHERE clause, the following operators can be used:

Operator Description

= Equal

<> Not equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

BETWEEN Between an inclusive range

LIKE Search for a pattern

IN If you know the exact value you want to return for at least one of the columns

SQL AND & OR OperatorsThe AND & OR operators are used to filter records based on more than one condition.SQL WildcardsSQL wildcards can substitute for one or more characters when searching for data in a database.SQL wildcards must be used with the SQL LIKE operator.With SQL, the following wildcards can be used:

Wildcard Description

% A substitute for zero or more characters

_ A substitute for exactly one character

[charlist] Any single character in charlist

[^charlist] Any single character not in charlistWHERE Clause Example

SELECT * FROM Employee WHERE Emp_Code=1001 SELECT * FROM Employee WHERE Emp_Name=’Ali’ SELECT * FROM Employee WHERE Salary>=15000 SELECT * FROM Employee WHERE Salary BETWEEN 15000 and 25000 SELECT * FROM Employee WHERE Emp_Name LIKE ‘A%’ SELECT * FROM Employee WHERE Emp_Name LIKE ‘A_’ SELECT * FROM Employee WHERE Emp_Name LIKE ‘[AGR]%’ SELECT * FROM Employee WHERE Dept_Code IN(10,30,50) SELECT * FROM Employee WHERE First_Name=”Ali’ AND Last_Name=’Ahmed’ SELECT * FROM Employee WHERE First_Name=”Ali’ OR Last_Name=’Ahmed’

Page 3: Microsoft Visual Basic 6 (Database Programming SQL)

Database Programming With Visual Basic 6SQL ORDER BY Clause

The ORDER BY keyword is used to sort the result-set.

Page 4: Microsoft Visual Basic 6 (Database Programming SQL)

Database Programming With Visual Basic 6SQL ORDER BY Syntax

SELECT column_name(s) FROM table_nameORDER BY column_name(s) ASC|DESC

ORDER BY Example SELECT * FROM Employee ORDER BY Dept_Code SELECT * FROM Employee ORDER BY salary DESC,Dept_Code

SQL TOP ClauseThe TOP clause is used to specify the number of records to return.

SQL Server SyntaxSELECT TOP number|percent column_name(s) FROM table_name

SQL TOP Example SELECT TOP 5 FROM Employee SELECT TOP 50 PERCENT * FROM Employee

SQL AliasYou can give a table or a column another name by using an alias. This can be a good thing to do if you have very long or complex table names or column names.

SQL Alias Syntax SELECT column_name AS alias_name FROM table_name AS alias_name

Alias ExampleSELECT e.Emp_Code [Employee Code],e.Emp_Name ‘Employee Name’ FROM

Employee eSQL Joins

SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.

Different SQL JOINsBefore we continue with examples, we will list the types of JOIN you can use, and the differences between them.

INNER JOIN: Return rows when there is at least one match in both tables LEFT JOIN: Return all rows from the left table, even if there are no matches in

the right table RIGHT JOIN: Return all rows from the right table, even if there are no matches

in the left table FULL JOIN: Return rows when there is a match in one of the tables

SQL INNER JOIN SyntaxSELECT column_name(s)FROM table_name1INNER JOIN table_name2ON table_name1.column_name =table_name2.column_name

SQL LEFT JOIN SyntaxSELECT column_name(s)FROM table_name1LEFT JOIN table_name2ON table_name1.column_name =table_name2.column_name

SQL RIGHT JOIN SyntaxSELECT column_name(s)FROM table_name1RIGHT JOIN table_name2ON table_name1.column_name =table_name2.column_name

SQL FULL JOIN SyntaxSELECT column_name(s)FROM table_name1FULL JOIN table_name2ON table_name1.column_name =table_name2.column_name

Page 5: Microsoft Visual Basic 6 (Database Programming SQL)

Database Programming With Visual Basic 6SQL INNER JOIN ExampleSELECT * FROM Employee e INNER JOIN Department d ON e.Dept_Code=d.Dept_CodeSQL LEFT JOIN ExampleSELECT * FROM Employee e LEFT JOIN Department d ON e.Dept_Code=d.Dept_CodeSQL RIGHT JOIN ExampleSELECT * FROM Employee e RIGHT JOIN Department d ON e.Dept_Code=d.Dept_CodeSQL FULL JOIN ExampleSELECT * FROM Employee e FULL JOIN Department d ON e.Dept_Code=d.Dept_CodeSQL UNION or UNION ALL OperatorSQL UNION operator combines two or more SELECT statements. The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL. The column names in the result-set of a UNION are always equal to the column names in the first SELECT statement in the UNION.SQL UNION or UNION ALL Syntax

SELECT column_name(s) FROM table_name1UNION or UNION ALLSELECT column_name(s) FROM table_name2

SQL UNION ExampleSELECT Dept_Code,Dept_Name FROM DepartmentUNIONSELECT Emp_Code,Emp_Name FROM Employee

SQL NULL ValuesNULL values represent missing unknown data.By default, a table column can hold NULL values.SQL FunctionsSQL has many built-in functions for performing calculations on data.SQL Aggregate FunctionsSQL aggregate functions return a single value, calculated from values in a column.Useful aggregate functions:

AVG() - Returns the average value COUNT() - Returns the number of rows MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum

SQL Scalar functionsSQL scalar functions return a single value, based on the input value.Useful scalar functions:

UCASE() - Converts a field to upper case LCASE() - Converts a field to lower case MID() - Extract characters from a text field LEN() - Returns the length of a text field ROUND() - Rounds a numeric field to the number of decimals specified NOW() - Returns the current system date and time

SQL GROUP BY StatementThe GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

Page 6: Microsoft Visual Basic 6 (Database Programming SQL)

Database Programming With Visual Basic 6SQL GROUP BY Syntax

SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name

SQL GROUP BY ExampleSELECT d.Dept_Name,SUM(e.Salary) Salary FROM Employee e INNER JOIN Department d ON e.Dept_Code=d.Dept_Code GROUP BY d.Dept_NameSQL HAVING ClauseThe HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.SQL HAVING Syntax

SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_nameHAVING aggregate_function(column_name) operator value

SQL HAVING ExampleSELECT d.Dept_Name,SUM(e.Salary) Salary FROM Employee e INNER JOIN Department d ON e.Dept_Code=d.Dept_Code GROUP BY d.Dept_Name HAVING SUM(e.Salary) >15000SQL INSERT INTO StatementThe INSERT INTO statement is used to insert a new row in a table.SQL INSERT INTO Syntax

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

SQL INSERT INTO ExampleINSERT INTO Department (Dept_Code,Dept_Name) VALUES (60,’Sales’)SQL UPDATE StatementThe UPDATE statement is used to update records in a table. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!SQL UPDATE Syntax

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

SQL UPDATE ExampleUPDATE Department SET Dept_Name=’Sales and Marketing’ WHERE Dept_Code=60SQL DELETE StatementThe DELETE statement is used to delete records in a table. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deletedSQL DELETE Syntax

DELETE FROM table_name WHERE some_column=some_valueSQL DELETE ExampleDELETE FROM Employee WHERE Dept_Code=30DELETE FROM Employee (Delete All Records)