CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los...
-
date post
21-Dec-2015 -
Category
Documents
-
view
219 -
download
5
Transcript of CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los...
![Page 1: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/1.jpg)
CS320 Web and Internet ProgrammingSQL and MySQL
Chengyu SunCalifornia State University, Los Angeles
![Page 2: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/2.jpg)
Web and Databases
E-commerce sites Products, order, customers
News sites Subscribers, articles
Web boards Users, postings
… anywhere where a large amount of information needs to be managed safely and efficiently
![Page 3: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/3.jpg)
Database vs. File
SQLFaster searchACID Atomicity Consistency Isolation Durability
![Page 4: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/4.jpg)
Relational Model
Proposed by Edgar F. Codd in early 1970’sAll major DBMS are relational (and the good ones are object-relational)
![Page 5: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/5.jpg)
A Relational DB Example
OID CID ODATE SDATE
1 1 4/29/2005
NULL
2 2 3/20/2005
3/37/2005
OID PID Quantity
1 1 2
1 5 2
2 2 1
CID FNAME
LNAME ADDRESS
1 Chengyu
Sun Street #215
2 Steve Sun Street #711
PID Description
Price
1 Intel P4 $200
2 Intel P3 $49
3 AthlonXP $100
4 ASUS $128
5 TYAN $400
orders
products
order_details
customers
![Page 6: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/6.jpg)
Terminology
DBMSDB DB
DB
Database
Schema
Schema
Schema
Schema
• Relations (tables)• Views, indexes• Procedures and triggers• …
![Page 7: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/7.jpg)
DBMS
Database Management System (DBMS) is a software that manages databasesCommon DBMS Commercial – Oracle, IBM DB2, MS
SQL Server, Access Open source – MySQL, PostgreSQL
![Page 8: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/8.jpg)
Database and Schema
A database is a collection of data managed by a DBMSA database contains one or more schemasA schema contains a number of schema elements, such as tables, indexes, stored procedures, and so on
![Page 9: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/9.jpg)
More Terminology
student_id name
1001 John Doe
1002 Jane Doe
Attributes (fields, columns)
Rows(Records)(Tuples)
students
Table (relation) schema:students( student_id, name )
Table (relation)
Database schema: database name + table schemas
![Page 10: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/10.jpg)
SQL
Structured Query LanguageStandard query language of relational databasesSupported by all major relational databases with some variations
![Page 11: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/11.jpg)
SQL Script
A text file contains SQL statements and comments Statements: select, insert, create … Comments
lines started with -- MySQL also supports C-style comment
syntax, i.e. /* */
Usually uses the .sql suffix
![Page 12: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/12.jpg)
MySQL
Very popular in web development Open source Very fast search Full text indexing and search Developer-friendly features
drop table if exists insert ... on duplicate key update /* */ ...
![Page 13: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/13.jpg)
Databases in MySQL
MySQL Server
• tables• indexes• constraints• views• ...
database database
• user information• access privileges
mysql
In MySQL, schema = database
![Page 14: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/14.jpg)
MySQL on the CS3 Server
Version 5.0.45One database per account DB name is the same as the server
account username. E.g. cs320stu31 Username and password are the
same as the ones for the server account
![Page 15: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/15.jpg)
Connect to a MySQL Database
HostPort (default 3306)DatabaseUsernamePassword
![Page 16: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/16.jpg)
Connect to Your MySQL Database on CS3
http://sun.calstatela.edu/~cysun/www/teaching/servers/cs3.html#MySQL Command line client mysql MySQL Query Browser phpMyAdmin
Change password set password = password
(‘something');
![Page 17: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/17.jpg)
Run SQL Scripts
Command line client \. path/to/script.sql source path/to/script.sql;
MySQL Query Browser File Open Script..., then Execute
phpMyAdmin Import
Format of the imported file: SQL
![Page 18: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/18.jpg)
Create a Table
create table table_name ( field_name field_type [NOT NULL] [UNIQUE] [DEFAULT value], field_name field_type [NOT NULL] [UNIQUE] [DEFAULT value], … [PRIMARY KEY(field_name, …)]);
create table products ( prod_id char(8) not null, -- product id description text, -- product description price decimal(12,2), -- price primary key (prod_id));
![Page 19: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/19.jpg)
Field Types
Numerical types int, float, double, decimal(m,n)
String types char(n), varchar(n)
Date and time date, time, datetime, timestamp
‘YYYY-MM-DD hh:mm:ss’
![Page 20: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/20.jpg)
Auto Increment Field
create table users (id int auto_increment primary key,username varchar(64) not null unique,password char(16)
);
insert into users (username,password) values (‘cysun’,’abcd’);insert into users (username,password) values (‘csun’,’xyz’);
![Page 21: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/21.jpg)
Populate Tables
insert into orders values (1000, 1, ‘2004-04-29’, ‘2004-05-01’);insert into orders (oid, cid, odate) values (1001, 2, ‘2004-05-01’);
insert into table values (value1, value2, …);
insert into table (field, …) values (value, …);
![Page 22: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/22.jpg)
Search for Records
select description, price from products;select * from products;select * from products where price < 300;select * from products where prod_id = ‘cpu-0001’;
select field(s) from table(s) where condition(s);
![Page 23: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/23.jpg)
Pattern Matching
LIKE, REGEXP % -- any zero or more characters . – any single charater [abc], [a-z], [0-9] – range * -- zero or more instances of the preceding
character ^ -- beginning of a string $ -- end of a string
select * from products where description like ‘%intel%’;
![Page 24: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/24.jpg)
Update Records
update table set field=value [, …]where condition(s);
update products set price=320 where prod_id = ‘cpu-0001’;update products set price=200, description=‘Intel Pentium M 1.7GHz’ where prod_id = ‘cpu-0001’;
![Page 25: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/25.jpg)
Delete Records
delete from orders;delete from orders where odate < ‘2005-12-31’ and sdate is not null;
delete from table where condition(s);
![Page 26: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/26.jpg)
Delete Tables and Databases
Delete a database drop database cs320stu31; -- don’t do
this!
Delete a table drop table products; drop table if exists products; -- MySQL
only
![Page 27: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/27.jpg)
Schema Design Example …
Customer, Product, Order
public class Customer { int id; String lastName; String firstName; String address;}
public class Product { int id; String description; double price;}
![Page 28: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/28.jpg)
… Schema Design Example
public class Order { int id; Date dateOrdered; Date dateShipped;
Customer customer; Map<Product, int> products;}
![Page 29: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/29.jpg)
Simple Schema Design Rules
OO Relational
Class Table
Class variables Attributes
Java types SQL types
References ID
Collection New Table
![Page 30: CS320 Web and Internet Programming SQL and MySQL Chengyu Sun California State University, Los Angeles.](https://reader036.fdocuments.us/reader036/viewer/2022062715/56649d6d5503460f94a4d54a/html5/thumbnails/30.jpg)
Exercises
Read MySQL Reference Manual String functions Date and time functions