Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL Basics BCHB697.

Similar presentations


Presentation on theme: "SQL Basics BCHB697."— Presentation transcript:

1 SQL Basics BCHB697

2 Outline Structured Query Language (SQL)
Database / Table creation, modification Physical data model implementation Instance insertion Physical data model population Information retrieval Physical data model queries Information update, deletion BCHB697 - Edwards

3 SQL: Structured Query Language
…for relational database management systems Each DBMS has its own variant of SQL The core is pretty consistent Minor syntax details can vary (avoid these) Specialized datatypes, internals (vendor lock-in) SQL commands/queries for what not how: Database creation, data insert, data retrieval BCHB697 - Edwards

4 SQL Syntax Elements Command end: Schema, table, context:
Semicolon: command; Schema, table, context: Period: schema.table.column Quotes for table, column names: Backticks: `name with spaces` (Optional) Quotes for strings: Single quotes: 'string' Double quotes: "some dbms" (MySQL) Comments: Double dash (space): -- C-style: /* multiple-lines */ Hash: # (MySQL) BCHB697 - Edwards

5 Physical data model creation
Often done once, using a UI (myphpadmin) Under the hood, SQL commands do the work We can execute these directly. We can dump the SQL commands to a file Save the commands to recreate the physical data model in a file We can 'inspect' the physical data-model to infer the logical data-model. BCHB697 - Edwards

6 Physical data model creation
create schema <dbname>; use <dbname>; show databases; drop schema <dbname>; DROP SCHEMA IF EXISTS sakila; CREATE SCHEMA sakila; USE sakila; BCHB697 - Edwards

7 Physical data model creation
create table <tablename> ( <column spec>, <column spec>, … ) <table options>; CREATE TABLE actor ( actor_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, first_name VARCHAR(45) NOT NULL, last_name VARCHAR(45) NOT NULL, last_update TIMESTAMP NOT NULL, PRIMARY KEY (actor_id), KEY idx_actor_last_name (last_name) )ENGINE=InnoDB DEFAULT CHARSET=utf8; BCHB697 - Edwards

8 Physical data model inspection / cleanup
show tables; show create table <tablename>; describe <tablename>; truncate table <tablename>; drop table <tablename>; BCHB697 - Edwards

9 Physical data model population
insert into <tablename> (<col1>, <col2>, <col3>) values (value1,1, value1,2, value1,3), …, (value2,1, value2,2, value2,3); Data types must match! We can add one or many rows per insert. Column names optional if all values are specified in the correct order. Columns may be left unspecified: AUTO_INCREMENT and DEFAULT values inserted Table constraints, if any, will be checked BCHB697 - Edwards

10 Physical data model population
INSERT INTO `name` (`id`, `taxonomy_id`, `name`, `name_class`) VALUES (1, 1, 'all', 'synonym'), (2, 1, 'root', 'scientific name'), (3, 2, 'Eucarya', 'synonym'), (4, 2, 'Eucaryotae', 'synonym'), (5, 2, 'Eukarya', 'synonym'); INSERT INTO actor VALUES (1,'PENELOPE','GUINESS',' :34:33'), (2,'NICK','WAHLBERG',' :34:33'), (3,'ED','CHASE',' :34:33'), (4,'JENNIFER','DAVIS',' :34:33'); BCHB697 - Edwards

11 Physical data model population
Load tabular data from a file: load data infile '<local-filename>' Often this is carried out using a user-interface (myphpadmin) Usually the headers (CSV,TSV) specify the column names …but matching schema column order is sufficient BCHB697 - Edwards

12 Information retrieval
Generic form: select <column(s)> from <schema>.<tablename> where <condition(s)> <modifier(s)>; Simple form for all rows from a table: select * from <tablename>; Omit <schema> unless needed. * indicates all columns from table. BCHB697 - Edwards

13 Information retreival
use taxonomy; SELECT * FROM name; select taxonomy.name.taxonomy_id from taxonomy.name where taxonomy.name.name = 'human'; select taxonomy_id from name where name = 'human'; BCHB697 - Edwards

14 Information retrieval
Conditions specified using: =, <, >, <=, >=, !=, <>, LIKE LIKE does string matching: zero, one, or more characters: % single character: _ Missing values can be matched with NULL Conditions can be chained by: AND, OR, NOT Use parentheses when necessary: NOT (first_name='bob' OR last_name='Smith') BCHB697 - Edwards

15 Information Retrieval
use sakila; select * from actor where first_name = 'ben' and last_name = 'willis'; from sakila.actor or last_name = 'willis'; where first_name like 'b%' BCHB697 - Edwards

16 Information Retrieval
Use DISTINCT to eliminate duplicates select last_name from actor where last_name like 'w%' select distinct last_name from actor BCHB697 - Edwards

17 Information Retrieval
Use count(*)to count the rows returned Use as <header> to rename columns select count(*) from actor where last_name like 'w%' select count(distinct last_name) as number from actor BCHB697 - Edwards

18 Information Retrieval
Also, sum, max, min… select sum(amount) from payment; select max(length) from film; BCHB697 - Edwards

19 Information Retrieval
Use limit <n> for max rows. Limit is applied last. select * from actor limit 10; select * from sakila.actor where first_name = 'b%' or last_name = 'w%' limit 1; select count(*) as number from actor where last_name like 'w%' BCHB697 - Edwards

20 Information Retrieval
Use order by to sort the table ASC (default) and DESC to indicate direction Columns separated by commas (,) select * from actor order by first_name, actor_id desc; select * from actor where first_name like 'b%' order by last_name limit 1; BCHB697 - Edwards

21 Information Update UPDATE <tablename> SET <col1>=value1, <col2>=value2 WHERE <condition(s)>; DANGEROUS! update actor set first_name = 'Bruce' where actor_id = 83 BCHB697 - Edwards

22 Information Delete DELETE FROM <tablename> WHERE <condition(s); DANGEROUS! delete from actor where actor_id = 83 BCHB697 - Edwards

23 Exercise Look at the following SQL dumps:
sakila-schema.sql, sakila-data.sql, taxonomy.sql Construct some interesting queries on the sakila database Longest, shortest movie? Most, least expensive movie to replace? Total replacement cost for the video library? Are there any customers at the same address? Are there any films with multiple categories, films with no category assignment? How many customers for each store? BCHB697 - Edwards


Download ppt "SQL Basics BCHB697."

Similar presentations


Ads by Google