没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Introduction to Databases & SQL
CSCE 156 - Introduction to Computer Science II
Christopher M. Bourke
cbourke@cse.unl.edu
Introduction to Databases & SQL
I
Lifetime of a program is short-lived
I
Applications perform small ephemeral operations
I
Can crash and die
I
Programs may be limited to sessions or even single requests
I
Need a way to persist data or program state across program lives
I
Databases provide such a means
Motivating Example I
Flat Files
Consider the following data, stored in a flat file:
Course Course Name Student NUID Email
waits, tom 11223344 tomwaits@hotmail.com
CSCE 156 Intro to CSII Lou Reed 11112222 reed@gmail.com
CSCE 156 Intro to CSII Tom Waits 11223344 twaits@email.com
CSCE 230 Computer Hardware Student, J. 12345678 jstudent@geocities.com
CSCE 156 Intro to CSII Student, John 12345678 jstudent@geocities.com
CSCE 230 Computer Hardware Student, J. 12345678 jstudent@geocities.com
CSCE 235 Discrete Math Student, John 12345678 jstudent@geocities.com
CSCE 235 Discrete Math Tom Waits 11223344 twaits@email.com
NONE Null Tom Waits 11223344 twaits@email.com
Table : Course enrollment data
Motivating Example II
Flat Files
Problems?
I
Repetition of data
I
Incomplete data
I
Integrity of data
I
Organizational problems: any aggregation requires processing all
records
I
Updating information is difficult (must enumerate all possible
changes, side effects so that information is not lost)
I
Formatting Issues
I
Concurrency Issues
Relational Databases I
Key Aspects
Solution: Relational Database Systems (RDBS) or Relational Database
Management System (RDMS)
I
Stores data in tables
I
Tables have a unique name and type description of its fields (integer,
string)
I
Each column stores a single piece of data (field)
I
Each row represents a record (or object!)
Relational Databases II
Key Aspects
I
Each row may have a unique primary key which may be
I
Automatically incremented
I
An external unique identifier: SSN, ISBN, NUID
I
Based on a combination of fields (Geographical data)
I
Rows in different tables are related to each other through foreign keys
I
Order of rows/columns is meaningless
Relational Databases III
Key Aspects
I
Supports Transactions: an interaction or batch of interactions treated
as one unit
I
Constraints
I
Allowing or disallowing NULL
I
Disallowing “bad” values (ranges)
I
Enforcing formatting (capitalization, precision)
I
Limiting combinations of data fields
Relational Databases IV
Key Aspects
I
ACID principles
I
Atomicity – Data transactions must be an all-or-nothing process
I
Atomic operation: not divisible or decomposable
I
Consistency – Transactions will retain a state of consistency
I
All constraints, triggers, cascades preserve a valid state after the
transaction has completed
I
Isolation – No transaction interferes or is even aware of another; state
transitions are equivalent to serial transactions
I
Durability – Once committed, a transaction remains so
I
Data is to be protected from catastrophic error (power loss/crash)
Commercial RDBMs
I
MS Access :)
I
MySQL (owned by Oracle, released under GNU GPL)
I
PostgreSQL (true FOSS!)
I
Informix (IBM)
I
DB2 (IBM)
I
SQLServer (Microsoft)
I
Oracle Database
I
SQLite
Others:
I
Google’s BigTable → Spanner
I
Apache Cassandra (Facebook)
I
Amazon’s Dynamo
Advantages I
I
Data is structured instead of “just there”
I
Better organization
I
Duplication is minimized (with proper normalization)
I
Updating information is easier
I
Organization of data allows easy access
I
Organization allows aggregation and more complex information
Advantages II
I
Data integrity can be enforced (data types and user defined
constraints)
I
Faster
I
Scalable
I
Security
I
Portability
I
Concurrency
Structured Query Language
We interact with RDBMs using Structured Query Language (SQL)
I
Common language/interface to most databases
I
Developed by Chamberlin & Boyce, IBM 1974
I
Implementations may violate standards: portability issues
I
Comments: -- or # (MySQL on cse)
I
Create & manage tables: CREATE, ALTER, DROP
I
Transactions: START TRANSACTION, ROLLBACK, COMMIT
Structured Query Language
CRUD
Basic SQL functionality: CRUD:
I
Create – insert new records into existing tables
I
Retrieve – get a (subset) of data from specific rows/columns
I
Update – modify data in fields in specified rows
I
Destroy – delete specific rows from table(s)
Misc RDMS Issues I
Important aspects that will be omitted (good advanced topics):
Views – RDBSs allow you to create view of data; predefined select
statements that aggregate (or limit) data while appearing to be a separate
table to the end user
Triggers – SQL routines that are executed upon predefined events
(inserts/updates) in order to create side-effects on the database
Misc RDMS Issues II
Stored Procedures – SQL routines (scripts) that are available to the end
user
Temp Tables – Temporary tables can be created to store intermediate
values from a complex query
Nested Queries – SQL supports using subqueries to be used in other
queries
MySQL
Getting Started
You have access to a MySQL database on cse
I
Database name: your cse login
I
Password: see the system FAQ,
http://cse.unl.edu/systems-faqs
I
Option 1: Command Line Interface (CLI): >mysql -u cselogin -p
I
Option 2: MySQL Workbench
(http://www.mysql.com/products/workbench/)
Useful MySQL commands (not general SQL) to get you started:
I
USE dbdname;
I
SHOW TABLES;
I
DESCRIBE tablename;
Creating Tables
Syntax:
1 CREATE TABLE table_name (
2 field_name fieldType [options],
3 ...
4 PRIMARY KEY (keys)
5 );
Options:
I
AUTO_INCREMENT (for primary keys)
I
NOT NULL
I
DEFAULT (value)
Column Types
I
VARCHAR(n) – variable character field (or CHAR , NCHAR , NVCHAR –
fixed size character fields)
I
INTEGER or INT
I
FLOAT (or DOUBLE , REAL , DOUBLE PRECISION )
I
DECIMAL(n,m) , NUMERIC(n,m) (max total digits, max decimal digits)
I
Date/Time functions: rarely portable
I
MySQL: see http://dev.mysql.com/doc/refman/5.0/en/
date-and-time-functions.html
剩余10页未读,继续阅读
资源评论
SQL必知必会
- 粉丝: 11
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功