Introduction toI Structured Query Language I Usually “talk” to a database server I Used as front end to many databases mysql,postgresql, oracle, sybase I Three Subsystems: data descripti
Trang 2SQL
Trang 3Introduction to
I Structured Query Language
I Usually “talk” to a database server
I Used as front end to many databases (mysql,postgresql, oracle, sybase)
I Three Subsystems: data description, data access andprivileges
I Optimized for certain data arrangements
I The language is case-sensitive, but I use upper casefor keywords
Trang 4Introduction to
I Structured Query Language
I Usually “talk” to a database server
I Used as front end to many databases (mysql,postgresql, oracle, sybase)
I Three Subsystems: data description, data access andprivileges
I Optimized for certain data arrangements
I The language is case-sensitive, but I use upper casefor keywords
Trang 5Introduction to
I Structured Query Language
I Usually “talk” to a database server
I Used as front end to many databases (mysql,postgresql, oracle, sybase)
I Three Subsystems: data description, data access andprivileges
I Optimized for certain data arrangements
I The language is case-sensitive, but I use upper casefor keywords
Trang 6Introduction to
I Structured Query Language
I Usually “talk” to a database server
I Used as front end to many databases (mysql,postgresql, oracle, sybase)
I Three Subsystems: data description, data access andprivileges
I Optimized for certain data arrangements
I The language is case-sensitive, but I use upper casefor keywords
Trang 7Introduction to
I Structured Query Language
I Usually “talk” to a database server
I Used as front end to many databases (mysql,postgresql, oracle, sybase)
I Three Subsystems: data description, data access andprivileges
I Optimized for certain data arrangements
I The language is case-sensitive, but I use upper casefor keywords
Trang 8Introduction to
I Structured Query Language
I Usually “talk” to a database server
I Used as front end to many databases (mysql,postgresql, oracle, sybase)
I Three Subsystems: data description, data access andprivileges
I Optimized for certain data arrangements
I The language is case-sensitive, but I use upper casefor keywords
Trang 9SQL What is SQL?
I Structured Query Language
I Usually “talk” to a database server
I Used as front end to many databases (mysql,postgresql, oracle, sybase)
I Three Subsystems: data description, data access andprivileges
I Optimized for certain data arrangements
I The language is case-sensitive, but I use upper casefor keywords
Trang 10Introduction to
I Multiple simultaneous changes to data (concurrency)
I Data changes on a regular basis
I Large data sets where you only need someobservations/variables
I Share huge data set among many people
I Rapid queries with no analysis
I Web interfaces to data, especially dynamic data
Trang 11Introduction to
I Multiple simultaneous changes to data (concurrency)
I Data changes on a regular basis
I Large data sets where you only need someobservations/variables
I Share huge data set among many people
I Rapid queries with no analysis
I Web interfaces to data, especially dynamic data
Trang 12Introduction to
I Multiple simultaneous changes to data (concurrency)
I Data changes on a regular basis
I Large data sets where you only need someobservations/variables
I Share huge data set among many people
I Rapid queries with no analysis
I Web interfaces to data, especially dynamic data
Trang 13Introduction to
I Multiple simultaneous changes to data (concurrency)
I Data changes on a regular basis
I Large data sets where you only need someobservations/variables
I Share huge data set among many people
I Rapid queries with no analysis
I Web interfaces to data, especially dynamic data
Trang 14Introduction to
I Multiple simultaneous changes to data (concurrency)
I Data changes on a regular basis
I Large data sets where you only need someobservations/variables
I Share huge data set among many people
I Rapid queries with no analysis
I Web interfaces to data, especially dynamic data
Trang 15Introduction to
I Multiple simultaneous changes to data (concurrency)
I Data changes on a regular basis
I Large data sets where you only need someobservations/variables
I Share huge data set among many people
I Rapid queries with no analysis
I Web interfaces to data, especially dynamic data
Trang 16SQL When do you need a Database?
I Multiple simultaneous changes to data (concurrency)
I Data changes on a regular basis
I Large data sets where you only need someobservations/variables
I Share huge data set among many people
I Rapid queries with no analysis
I Web interfaces to data, especially dynamic data
Trang 17I Backends to web sites
I Traditional rules may not be as important
Trang 18I Backends to web sites
I Traditional rules may not be as important
Trang 19I Backends to web sites
I Traditional rules may not be as important
Trang 20I Backends to web sites
I Traditional rules may not be as important
Trang 21I Backends to web sites
I Traditional rules may not be as important
Trang 22I Backends to web sites
I Traditional rules may not be as important
Trang 23I Backends to web sites
I Traditional rules may not be as important
Trang 24I Backends to web sites
I Traditional rules may not be as important
Trang 25Introduction to
I console command (mysql -u user -p dbname)
I GUI interfaces are often available
I Interfaces to many programming languages: R,python, perl, PHP, etc
I SQLite - use SQL without a database server
I PROC SQL in SAS
Trang 26Introduction to
I console command (mysql -u user -p dbname)
I GUI interfaces are often available
I Interfaces to many programming languages: R,python, perl, PHP, etc
I SQLite - use SQL without a database server
I PROC SQL in SAS
Trang 27Introduction to
I console command (mysql -u user -p dbname)
I GUI interfaces are often available
I Interfaces to many programming languages: R,python, perl, PHP, etc
I SQLite - use SQL without a database server
I PROC SQL in SAS
Trang 28Introduction to
I console command (mysql -u user -p dbname)
I GUI interfaces are often available
I Interfaces to many programming languages: R,python, perl, PHP, etc
I SQLite - use SQL without a database server
I PROC SQL in SAS
Trang 29Introduction to
I console command (mysql -u user -p dbname)
I GUI interfaces are often available
I Interfaces to many programming languages: R,python, perl, PHP, etc
I SQLite - use SQL without a database server
I PROC SQL in SAS
Trang 30SQL Ways to Use SQL
I console command (mysql -u user -p dbname)
I GUI interfaces are often available
I Interfaces to many programming languages: R,python, perl, PHP, etc
I SQLite - use SQL without a database server
I PROC SQL in SAS
Trang 31Introduction to
I A database server can contain many databases
I Databases are collections of tables
I Tables are two-dimensional with rows (observations)and columns (variables)
I Limited mathematical and summary operationsavailable
I Very good at combining information from severaltables
Trang 32Introduction to
I A database server can contain many databases
I Databases are collections of tables
I Tables are two-dimensional with rows (observations)and columns (variables)
I Limited mathematical and summary operationsavailable
I Very good at combining information from severaltables
Trang 33Introduction to
I A database server can contain many databases
I Databases are collections of tables
I Tables are two-dimensional with rows (observations)and columns (variables)
I Limited mathematical and summary operationsavailable
I Very good at combining information from severaltables
Trang 34Introduction to
I A database server can contain many databases
I Databases are collections of tables
I Tables are two-dimensional with rows (observations)and columns (variables)
I Limited mathematical and summary operationsavailable
I Very good at combining information from severaltables
Trang 35Introduction to
I A database server can contain many databases
I Databases are collections of tables
I Tables are two-dimensional with rows (observations)and columns (variables)
I Limited mathematical and summary operationsavailable
I Very good at combining information from severaltables
Trang 36SQL Some Relational Database Concepts
I A database server can contain many databases
I Databases are collections of tables
I Tables are two-dimensional with rows (observations)and columns (variables)
I Limited mathematical and summary operationsavailable
I Very good at combining information from severaltables
Trang 37Introduction to
Since a single server can support many databases, eachcontaining many tables, with each table having a variety
of columns, it’s easy to get lost when you’re working withdatabases These commands will help figure out what’savailable:
I SHOW DATABASES;
I SHOW TABLES IN database;
I SHOW COLUMNS IN table;
I DESCRIBE table; - shows the columns and theirtypes
Trang 38Introduction to
Since a single server can support many databases, eachcontaining many tables, with each table having a variety
of columns, it’s easy to get lost when you’re working withdatabases These commands will help figure out what’savailable:
I SHOW DATABASES;
I SHOW TABLES IN database;
I SHOW COLUMNS IN table;
I DESCRIBE table; - shows the columns and theirtypes
Trang 39Introduction to
Since a single server can support many databases, eachcontaining many tables, with each table having a variety
of columns, it’s easy to get lost when you’re working withdatabases These commands will help figure out what’savailable:
I SHOW DATABASES;
I SHOW TABLES IN database;
I SHOW COLUMNS IN table;
I DESCRIBE table; - shows the columns and theirtypes
Trang 40Introduction to
Since a single server can support many databases, eachcontaining many tables, with each table having a variety
of columns, it’s easy to get lost when you’re working withdatabases These commands will help figure out what’savailable:
I SHOW DATABASES;
I SHOW TABLES IN database;
I SHOW COLUMNS IN table;
I DESCRIBE table; - shows the columns and theirtypes
Trang 41SQL Finding Your Way Around the Server
Since a single server can support many databases, eachcontaining many tables, with each table having a variety
of columns, it’s easy to get lost when you’re working withdatabases These commands will help figure out what’savailable:
I SHOW DATABASES;
I SHOW TABLES IN database;
I SHOW COLUMNS IN table;
I DESCRIBE table; - shows the columns and theirtypes
Trang 42I INTEGER, SMALLINT, BIGINT
I NUMERIC(w,d), DECIMAL(w,d) - numbers with width
w and d decimal places
I REAL, DOUBLE PRECISION - machine and databasedependent
I FLOAT(p) - floating point number with p binarydigits of precision
Trang 43I INTEGER, SMALLINT, BIGINT
I NUMERIC(w,d), DECIMAL(w,d) - numbers with width
w and d decimal places
I REAL, DOUBLE PRECISION - machine and databasedependent
I FLOAT(p) - floating point number with p binarydigits of precision
Trang 44I INTEGER, SMALLINT, BIGINT
I NUMERIC(w,d), DECIMAL(w,d) - numbers with width
w and d decimal places
I REAL, DOUBLE PRECISION - machine and databasedependent
I FLOAT(p) - floating point number with p binarydigits of precision
Trang 45I INTEGER, SMALLINT, BIGINT
I NUMERIC(w,d), DECIMAL(w,d) - numbers with width
w and d decimal places
I REAL, DOUBLE PRECISION - machine and databasedependent
I FLOAT(p) - floating point number with p binarydigits of precision
Trang 46I INTEGER, SMALLINT, BIGINT
I NUMERIC(w,d), DECIMAL(w,d) - numbers with width
w and d decimal places
I REAL, DOUBLE PRECISION - machine and databasedependent
I FLOAT(p) - floating point number with p binarydigits of precision
Trang 47SQL Variable Types
SQL supports a very large number of different formats forinternal storage of information
Numeric
I INTEGER, SMALLINT, BIGINT
I NUMERIC(w,d), DECIMAL(w,d) - numbers with width
w and d decimal places
I REAL, DOUBLE PRECISION - machine and databasedependent
I FLOAT(p) - floating point number with p binarydigits of precision
Trang 48Introduction to
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 49Introduction to
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 50Introduction to
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 51Introduction to
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 52Introduction to
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 53Introduction to
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 54Introduction to
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 55Introduction to
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 56Introduction to
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 57SQL Variable Types (cont’d)
Character
I CHARACTER(L) - a fixed-length character of length L
I CHARACTER VARYING(L) or VARCHAR(L) - supportsmaximum length of L
Trang 58Introduction to
Suppose we have data measured on the height and weight
of children over a range of ages The first step is deciding
on the appropriate variable types, and creating the tablewith the CREATE TABLE command
CREATE TABLE kids(id CHAR(6),
race SMALLINT,age DECIMAL(6,3),height DECIMAL(7,3),weight DECIMAL(7,3),sex SMALLINT);
Trang 59SQL CREATE TABLE statement
Suppose we have data measured on the height and weight
of children over a range of ages The first step is deciding
on the appropriate variable types, and creating the tablewith the CREATE TABLE command
CREATE TABLE kids(id CHAR(6),
race SMALLINT,age DECIMAL(6,3),height DECIMAL(7,3),weight DECIMAL(7,3),sex SMALLINT);
Trang 60Introduction to
We could now enter individual items with the INSERTcommand:
INSERT INTO kids VALUES(100011,2,10.346,
148.5,38.95,1);
This quickly gets tedious We can automate the processusing the LOAD DATA command:
LOAD DATA INFILE ’kids.tab’
INTO TABLE kidsFIELDS TERMINATED BY ’\t’;This will read an entire tab-separated file into thedatabase in one command
Trang 61Introduction to
We could now enter individual items with the INSERTcommand:
INSERT INTO kids VALUES(100011,2,10.346,
148.5,38.95,1);
This quickly gets tedious We can automate the processusing the LOAD DATA command:
LOAD DATA INFILE ’kids.tab’
INTO TABLE kidsFIELDS TERMINATED BY ’\t’;
This will read an entire tab-separated file into thedatabase in one command
Trang 62SQL Entering observations into a table
We could now enter individual items with the INSERTcommand:
INSERT INTO kids VALUES(100011,2,10.346,
148.5,38.95,1);
This quickly gets tedious We can automate the processusing the LOAD DATA command:
LOAD DATA INFILE ’kids.tab’
INTO TABLE kidsFIELDS TERMINATED BY ’\t’;
This will read an entire tab-separated file into thedatabase in one command
Trang 63Introduction to
In SQL, the WHERE clause allows you to operate on subsets
of a table The following comparison operators areavaiable:
I Usual logical operators: < > <= >= = <>
I BETWEEN used to test for a range
I IN used to test group membership
I Keyword NOT used for negation
I LIKE operator allows wildcards
_ means single character, % means anything
I SELECT salary WHERE name LIKE ’Fred %’;
I RLIKE operator allows regular expressions
I Use AND(&&) and OR(||) to combine conditions