access query design using sql update
Post on 21-Jan-2015
2.198 Views
Preview:
DESCRIPTION
TRANSCRIPT
ACCESS QUERY ACCESS QUERY DESIGN USING SQLDESIGN USING SQL
Aryati BakriAryati Bakri
Categories of SQL StatementCategories of SQL Statement
DDL – Data Definition LanguageDDL – Data Definition Language– Defining the structure of the databaseDefining the structure of the database
DCL – Data Control LanguageDCL – Data Control Language– Used for controlling access to the Used for controlling access to the
database database DML – Data Manipulation LanguageDML – Data Manipulation Language
– For retrieving and updating the For retrieving and updating the database recordsdatabase records
DDL – Data Definition LanguageDDL – Data Definition Language
Create table – create tables, defining Create table – create tables, defining their structure in terms of fieldstheir structure in terms of fields
Alter table – change the structure Alter table – change the structure table (adding, removing or changing table (adding, removing or changing the fields)the fields)
Drop table – remove the tableDrop table – remove the table Create index- create index on one or Create index- create index on one or
more specified fields in a tablemore specified fields in a table Drop index – remove the index Drop index – remove the index
DCL – Data Control LanguageDCL – Data Control Language
Grant and Revoke – security issuesGrant and Revoke – security issues Commit and Rollback – transaction Commit and Rollback – transaction
processingprocessing Lock – concurrency control Lock – concurrency control
DML – Data Manipulation DML – Data Manipulation LanguageLanguage
InsertInsert UpdateUpdate DeleteDelete SelectSelect
CREATE TABLECREATE TABLE
Syntax:Syntax:CREATE TABLECREATE TABLE
( field1 type [(size)] [ Not Null] [index1]( field1 type [(size)] [ Not Null] [index1]
[, field1 type [(size)] [ Not Null] [index2][, field1 type [(size)] [ Not Null] [index2]
[,…] ] [,…] ]
[, CONSTRAINT multifieldindex [, …]][, CONSTRAINT multifieldindex [, …]]
StepsSteps
Click Queries tabClick Queries tab Create query in Create query in
design viewdesign view Close all tableClose all table In Menu Toolbar In Menu Toolbar
Select Query -> Select Query -> SQL Specific SQL Specific Data DefinitionData Definition
Example 1: create a customer tableExample 1: create a customer table
Create table customerCreate table customer
(c_no short not null constraint c1 primary (c_no short not null constraint c1 primary key,key,
Title text(5),Title text(5),
name text (25),name text (25),
address text (20),address text (20),
Cre_lim currency,Cre_lim currency,
balance currencybalance currency
))
Example 2: create the invoice table Example 2: create the invoice table which ‘references’ which ‘references’ the Customer tablethe Customer table
Create table invoice Create table invoice
(inv_no short not null constraint c1 primary (inv_no short not null constraint c1 primary key,key,
c_no short not null constraint c2 c_no short not null constraint c2 references customer(c_no),references customer(c_no),
Inv_date datetime,Inv_date datetime,
Amount currency) Amount currency)
Example 3: Create a payment table Example 3: Create a payment table linked to invoicelinked to invoice
Create table paymentCreate table payment(inv_no short,(inv_no short,pmt_no short, pmt_no short, pmt_date datetime,pmt_date datetime,amount currency,amount currency,constraint C1 primary key (inv_no,pmt_no),constraint C1 primary key (inv_no,pmt_no),constraint C2 foreign key (inv_no) references constraint C2 foreign key (inv_no) references
invoice (inv_no))invoice (inv_no))
Exercise 1: Create TableExercise 1: Create TableCreate the table below using SQL statement in MS Access.
Alter TableAlter Table
ADD COLUMN and DROP COLUMNADD COLUMN and DROP COLUMN
ADD CONSTRAINT: add single or ADD CONSTRAINT: add single or multiple indexmultiple index
DROP CONSTRAINT: delete a DROP CONSTRAINT: delete a multiple-field indexmultiple-field index
Alter tableAlter table
Syntax:Syntax:ALTER TABLE table {add {column field ALTER TABLE table {add {column field
types[(size)] types[(size)]
[NOT NULL] [ CONSTRAINT index] |[NOT NULL] [ CONSTRAINT index] |
[ CONSTRAINT multifieldindex] |[ CONSTRAINT multifieldindex] |
DROP {COLUMN field I CONSTRAINT DROP {COLUMN field I CONSTRAINT indexname} }indexname} }
Example 4: Add a ‘ phone’ field to Example 4: Add a ‘ phone’ field to the Customer tablethe Customer table
Alter table customerAlter table customer
Add column phone text[15]Add column phone text[15]
Example 5: Remove ‘phone’ field Example 5: Remove ‘phone’ field from table CUSTOMER from table CUSTOMER
Alter table customerAlter table customer
drop column phonedrop column phone
Example 6: Remove a relationship Example 6: Remove a relationship between Customer and Invoicebetween Customer and Invoice
Alter table invoiceAlter table invoice
drop constraint c2drop constraint c2
Drop tableDrop table
SyntaxSyntax– DROP TABLE tablenameDROP TABLE tablename
Example 7Example 7– Create table customer1 (cust_no, name, Create table customer1 (cust_no, name,
address)address)– Drop table customer1 (if the no Drop table customer1 (if the no
relationship between other table, for relationship between other table, for example 1 to M relationships).example 1 to M relationships).
Create IndexCreate Index
Syntax:Syntax:CREATE [UNIQUE] INDEX indexCREATE [UNIQUE] INDEX index
ON Table (field [ASC | DESC] , field [ASC | ON Table (field [ASC | DESC] , field [ASC | DESC],…])DESC],…])
[WITH {PRIMARY | DISALLOW NULL | [WITH {PRIMARY | DISALLOW NULL | IGNORE NULL}] IGNORE NULL}]
Example 8: Create an index on the Example 8: Create an index on the name field of the Customer Tablename field of the Customer Table
Create index nameCreate index name
On customer (name)On customer (name)
Example 9: Create an index on Example 9: Create an index on Invoice to speed up the joining of Invoice to speed up the joining of
customer and invoice customer and invoice
Create index c_noCreate index c_no
On invoice (c_no)On invoice (c_no)
Drop indexDrop index
SyntaxSyntax– DROP INDEX index on tableDROP INDEX index on table
Example 10:Example 10:– Drop index c_no on invoiceDrop index c_no on invoice
DATA MANIPULATION DATA MANIPULATION LANGUAGELANGUAGE
INSERT INTOINSERT INTO– SyntaxSyntax
INSERT INTO target [ (field1[, field2[,…]])] values INSERT INTO target [ (field1[, field2[,…]])] values (value1,[,value2[,…])(value1,[,value2[,…])
– Example 11Example 11 Add a new record to the Customer tableAdd a new record to the Customer table INSERT INTO customer values (4, 'Miss' , 'Lina' , 'Batu INSERT INTO customer values (4, 'Miss' , 'Lina' , 'Batu
Pahat', 1000,400) Pahat', 1000,400)
– Example 12Example 12 INSERT INTO customer (c_no, title, name, address, INSERT INTO customer (c_no, title, name, address,
Cre_lim, balance) values (5, 'Ms' , ‘Bakri' , ‘Muar', Cre_lim, balance) values (5, 'Ms' , ‘Bakri' , ‘Muar', 5000,500)5000,500)
Example 13: Add a new record to Example 13: Add a new record to the CUSTOMER Table. Leave the the CUSTOMER Table. Leave the
Cre_limCre_lim
INSERT INTO customer (c_no, title, INSERT INTO customer (c_no, title, name, address, balance) values (7, name, address, balance) values (7, 'Ms' , 'Abu' , 'Muar', 500)'Ms' , 'Abu' , 'Muar', 500)
Example 14: Add an invoice and Example 14: Add an invoice and payment record using a single payment record using a single
INSERT INTO commandINSERT INTO command
SELECT Invoice.*, payment.*SELECT Invoice.*, payment.*FROM invoice LEFT JOIN payment ON FROM invoice LEFT JOIN payment ON
invoice.inv_no = payment.inv_no;invoice.inv_no = payment.inv_no;
Save the query as Example 14Save the query as Example 14
Example 15: INSERT INTO Example 15: INSERT INTO command of Example14command of Example14
SyntaxSyntax
insert into Example14insert into Example14
(Invoice.inv_no,c_no,inv_date,invoice.a(Invoice.inv_no,c_no,inv_date,invoice.amount,payment.inv_no,pmt_no,pmt_mount,payment.inv_no,pmt_no,pmt_date,payment.amount) valuesdate,payment.amount) values
(10,1,5-2-06,100,10,1,5-2-06,100)(10,1,5-2-06,100,10,1,5-2-06,100)
Exercise 2: Insert IntoExercise 2: Insert Into
Using SQL command insert into the Using SQL command insert into the customer tablecustomer table
c_noc_no TitleTitle namename addressaddress Cre_limCre_lim balancebalance
22 MsMs JanaJana Johor BahruJohor Bahru $1,000.00$1,000.00 $400.00$400.00
33 MsMs LinaLina Batu PahatBatu Pahat $2,000.00$2,000.00 $400.00$400.00
44 MsMs MinahMinah Batu PahatBatu Pahat $2,000.00$2,000.00 $400.00$400.00
55 MrMr BakriBakri MuarMuar $6,250.00$6,250.00 $500.00$500.00
77 MrMr AbuAbu MuarMuar $500.00$500.00
INSERT INTO (Multiple records INSERT INTO (Multiple records append)append)
Example 16Example 16– Copy and paste table customer, rename Copy and paste table customer, rename
the new as Custthe new as Cust– insert into Cust select * from customerinsert into Cust select * from customer
Example 17Example 17– Copy and paste table customer, rename Copy and paste table customer, rename
the new as Customer3the new as Customer3– insert into Customer3 (c_no,name, insert into Customer3 (c_no,name,
address) select c_no,name,address address) select c_no,name,address from customer1 where address = 'Muar';from customer1 where address = 'Muar';
DML: UPDATEDML: UPDATE
SyntaxSyntaxUPDATE tableUPDATE table
SET newvalueSET newvalue
Where criteriaWhere criteria
Example 18: increase the credit Example 18: increase the credit limit of the Muar customer by 5% limit of the Muar customer by 5%
UPDATE customer SET cre_lim = cre_lim UPDATE customer SET cre_lim = cre_lim * 1.05 WHERE address = 'Muar' * 1.05 WHERE address = 'Muar'
Example 19: Increase the credit Example 19: Increase the credit limit of customer 3,4,5 by 1000limit of customer 3,4,5 by 1000
UPDATE customer SET cre_lim = UPDATE customer SET cre_lim = cre_lim + 1000 cre_lim + 1000
WHERE c_no in (3,4,5);WHERE c_no in (3,4,5);
DELETEDELETE
SyntaxSyntaxDELETEDELETE
FROM tableFROM table
WHERE criteriaWHERE criteria
Example 20: Delete the customerExample 20: Delete the customer
Delete from customer where balance Delete from customer where balance = 500;= 500;
Exercise 3Exercise 3
1.1. Write a query that displays for each Write a query that displays for each customer their customer code, name, customer their customer code, name, and total balance (from Customer table).and total balance (from Customer table).
2.2. Write a query that displays for each Write a query that displays for each customer their customer code, name, customer their customer code, name, total balance (from Customer table), and total balance (from Customer table), and their total purchases (from Invoice). This their total purchases (from Invoice). This column can be called Total_purchases for column can be called Total_purchases for instance.instance.
3.3. Write a query that generates the total Write a query that generates the total number of invoices, the invoice total for number of invoices, the invoice total for all of these invoices (total sales). all of these invoices (total sales).
Exercise 4Exercise 4
Create the customer table that Create the customer table that contain information belowcontain information below
CompanyNameCompanyName ContactNameContactName AddressAddress TelefonTelefon
NazaNaza AbuAbu Batu PahatBatu Pahat 075684569075684569
ProtonProton ZainalZainal Kuala LumpurKuala Lumpur 035421987035421987
ProduaProdua BakriBakri Johor BahruJohor Bahru 076421389076421389
KiaKia FirdausFirdaus Kuala LipisKuala Lipis 096754259096754259
HyundaiHyundai MijanMijan PekanPekan 097649754097649754
HondaHonda NurulNurul KuantanKuantan 095684328095684328
ToyotaToyota AzmiAzmi PontianPontian 076543890076543890
BMWBMW ManMan MelakaMelaka 068749765068749765
VolvoVolvo YaniYani SerembanSeremban 063456789063456789
Run the SQL commandRun the SQL command
SELECT * FROM customersSELECT * FROM customers SELECT CompanyName, ContactName SELECT CompanyName, ContactName
FROM customers FROM customers SELECT * FROM customers WHERE SELECT * FROM customers WHERE
companyname LIKE 'a%' companyname LIKE 'a%' SELECT CompanyName, ContactName SELECT CompanyName, ContactName
FROM customers WHERE CompanyName > FROM customers WHERE CompanyName > ‘d' AND ContactName > ‘n' ‘d' AND ContactName > ‘n'
Discuss the output with your friendsDiscuss the output with your friends
top related