Академический Документы
Профессиональный Документы
Культура Документы
@Instructor by Ngo Trung Kien Date: Thursday, September 09, 2010 RDBMS and Data Management
Inserting
Inserting SET
INSERT INTO table_name SET Field_1= value_1, Field_2=value_2,,;
Variable in T-SQL
Syntax Local DECLARE{ @local_name [as] Data_type, @local_name [as] Data_type,...} local_name: Name of variable data_type: is a data type of system or user define Example DECLARE @student_id char[10] DECLARE @proId int Global: that are defined and maintained by the sytems. @@VERSION
Comments in SQL
The standard SQL comment is two hyphens (--). However, some databases use other forms of comments as shown in the table below.
Introduction
The SELECT statement is discussed in order to retrieve and access data from database That data that is retrieved is not only useful for the purpose of reading but also for modification by operator as UPDATE, DELETE, In a table data can be viewed using the SELECT statement will display the required information in table The SELECT statement retrieves rows and columns from one or more table The Ouput of the SELECT statement is another table called result set The columns appear in the same sequence as the order of expression in the SELECT statement.
The SELECT statement also joins two table, or retrieves a subset of columns from one or more tables The SELECT statement also defines the colums to be used for a query The SELECT statement can consist of a series of expressions seperates by commas
collection: list of column name in any table (include between columns , if retrieves than one) or any (function, variable,..) table name: List of tabl Expression: Operators and comparison
SELECT * FROM tblCategories SELECT cat_id ,cat_name ,cat_img FROM tblCategories SELECT c.cat_id as ID ,c.cat_name as category_name FROM tblCategories as c WHERE c.cat_id=PA001 SELECT LEFT(C0909K,3)
10
SELECT INTO
INTO clause creates a new table and insert rows, colums listed in the SELECT INTO clause inserts existing rows into the new table
SELECT <list colums> INTO <new table> FROM <table selected>
SELECT DISTINCT
DISTINCT keyword elimitnates rows that are repeateing from the result set of a SELECT statement Syntax:
SELECT DISTINCT <column eliminate> FROM <table name>
12
SELECT WHERE
WHERE used to conditionally select or limit records retrieved by the query Syntax: SELECT <collection> FROM <table name> WHERE <expression> Where: collection: list of column name in any table (include between columns , if retrieves than one) or any (function, variable,..) table name: List of table Expression: Operators
MySQL- DML & DCL
13
The different operators that can be used with the WHERE <Expression>
Operator
=,>=,<,>,<= < >, != AND, OR, NOT LIKE BETWEEN Not equal to WHERE price>=10.5 AND price<=20.5 OR price=21.0 Search for pattern %[_^string]% WHERE price BETWEEN 10.5 AND 20.5
Description
Compare operators
14
15
ORDER BY: Sort query result by one or more columns follow ASC, DESC GROUP BY: clause partition the result set into one or more subset, can be it uses with HAVING Syntax:
SELECT <collection> FROM <table name> WHERE <expression> ORDER BY <column ASC|DESC> GROUP BY <column> [HAVING <expression>]
16
SELECT * FROM tblCategories as c WHERE 1=1 ORDER BY c.cat_name ASC SELECT ord_id , cus_name , sum(pro_price*ord_quantity) as Totals FROM tblOrderDetails WHERE 1=1 GROUP BY cus_id
17
The LIMIT clause is used most effectively in a SELECT statement when it is used with an ORDER BY clause. The LIMIT clause takes two arguments, as the following syntax shows:
18
In a normalized database, groups of data are stored in individual tables, and relationships are established between those tables to link related data As a result, often when creating SELECT, UPDATE, or DELETE statements, you want to be able to access data in different tables to carry out an operation affected by those relationships The real power of a relational database emerges from the ability to combine multiple entities in a single operation
MySQL- DML & DCL
19
Using WHERE
Syntax: SELECT <collection columns> FROM <table_1, table_2> WHERE <table_1.c_key=table_2.c_key> c_key: is primary key or foreign key Table_1, table_2: Related 1-1, 1-N or M-N
20
SELECT C.ContactName ,C.City ,O.OrderDate O.RequiredDate FROM Customers as C, Orders as O WHERE C.CustomerID=O.CustomerID
21
Using Join
SQL supports the following JOIN syntaxes for the table references part of SELECT statements and multiple-table DELETE and UPDATE statements: Table references type:
22
Using Join[1]
Syntax
c_key: is primary key or foreign key Table_1, table_2: Related 1-1, 1-N or M-N
23
SELECT C.ContactName ,C.City ,O.OrderDate O.RequiredDate FROM Customers as C INNER JOIN Orders as O ON C.CustomerID=O.CustomerID
24
25