160 likes | 265 Views
SQL statements. SQL statement types. Data definition Data manipulation Control. Data definition statements. CREATE DATABASE CREATE TABLE. CREATE DB. CREATE DATABASE ` db_name ` [ DEFAULT [ CHARACTER SET utf8 ] [ COLLATE utf8_lithuanian_ci ] ]
E N D
SQL statement types • Data definition • Data manipulation • Control
Data definition statements CREATE DATABASE CREATE TABLE
CREATE DB • CREATEDATABASE `db_name` [DEFAULT[CHARACTERSET utf8] [COLLATE utf8_lithuanian_ci] ] • http://dev.mysql.com/doc/refman/5.1/en/create-database.html • CREATE DATABASE `testing` • CREATE DATABASE `testing` DEFAULT CHARSET utf8 COLLATE utf8_lithuanian_ci
CREATE TABLE • CREATE TABLE `Supplier` ( `No` CHAR(5) NOT NULL, `Name` CHAR(20) NOT NULL, `State` SMALLINT, `City` CHAR(15),PRIMARY KEY (`Nr`) ) • Write a SQL statement to create a table for products
ALTER TAB:E • ALTER TABLE name • ADD • CHANGE • DROP • ALTER TABLE`supplier`ADD`founded`DATE • ALTER TABLE`supplier`CHANGE `founded``founded`TIMESTAMP • ALTER TABLE `supplier` DROP `founded`
DROP TABLE • DROP TABLE name • DROP TABLE `Detales`
Data manipulation statements INSERT SELECT UPDATE DELETE
INSERT • INSERT INTO <table> (<field1>, <field2>)VALUES (<value1>, <value2>) • INSERT INTO `supplier` (`no`, `name`, `state`, `city`)VALUES (1,’Company’, 20, ‘Kaunas’) • INSERT INTO `supplier` (`no`, `name`, `state`, `city`)VALUES (2,’Company B’, 15, ‘Kaunas’),VALUES (3,’Company C’, 10, ‘Vilnius’),
SELECT • SELECT <fields> FROM <table> • SELECT`name`, `state`FROM`supplier` • All entries in table supplier
SELECT • SELECT <fields> FROM <table> [WHERE <clause>] • SELECT `name`, `state` FROM `supplier` WHERE(`city`='Vilnius‘) OR (`city`=‘Kaunas‘)
SELECT • SELECT <fields> FROM <table> [WHERE <clause>] [ORDER BY <fields> ] • SELECT `name`, `state` FROM `supplier` WHERE `state`>5ORDER BY `city` DESC
SELECT • SELECT <fields> FROM <table> [WHERE <clause>] [ORDER BY <fields> ] • [LIMIT [<start>], <count> ] • SELECT `name`, `state` FROM `supplier` WHERE `state`>5ORDER BY `city` DESC • LIMIT 1, 3
UPDATE • UPDATE <table> SET <field1>=<value1>[, <field2>=<value2> ] [WHERE <clause>] [LIMIT [<start>], <count> ] • UPDATE `supplier`SET `stare`=50WHERE `city`=‘Vilnius’
DELETE • DELETEFROM <table> [WHERE <clause>] [LIMIT [<start>], <count> ] • DELETE FROM `supplier` WHERE `No`=3LIMIT 1
Exercise • Download sample database: • tiny.lt/sql1 • Import it to MySQL • Write SQL statements to select information for the following questions • Save statements to a text file • All clients from London • Employee list, sorted by last name • List of orders made in 2003 • Average price of 1:10 scale products • Sum of payments, made in 2004 by client no. 114 arunas@tiny.lt arunas@tiny.lt