Chapter 4: SQL Basics ALT_ENTER Will Maximize Window in Command Line Mode o SQL Basics Structured Query Language Used to create & extract data in a database environment SQL history Began as SEQUEL o Structured English Query Language o IBM 1974 o 1 st commercial role as query language for Oracle Late 70 s o 1980 s became de facto standard for RDBMS industry o 1989 ANSI standard SQL89 o 1992 SQL92 common standard for today o Most of today s databases add proprietary features and extensions to the SQL92 standard (dialects) o www.mysql.com/doc/en/compatibility.html o Overview of SQL Commands formulated as statements Begin with action word / keyword (SELECT) End with semicolon (;) 3 broad categories of statements DDL -- Data Definition Language o Used to define the structure of the database Create tables / create indexes / relationships DML Data Manipulation Language o Control querying / data maintenance / joining tables DCL Data Control Language o Used to control permissions / security o Walk through of SQL basics This is an overview subsequent chapters will cover each topic in more detail Example Administrator o Catalogs Assign1 o Right click Edit Table (Player) Tools Query Browser o File Default Schema (Assign1) o Select * Player 1 st database terminology (again) Table
Record row -- tuple Field column attribute Primary key Entity Integrity Relationship combination of multiple tables o Data slice subset of data using relationship (later) Command Line Walkthrough Sign into Administrator Tools Command Line Client CREATE DATABASE o Create Database classroom; o Show databases; (verify your work) o Use classroom; (activate database to work with) o Show tables; (see if any data is there) ADD a table o CREATE TABLE Create table students (stu_id char(9) not null, Lname varchar(25) not null, Fname varchar (15) not null, Class char(2), Assign1 int(2), PRIMARY KEY (stu_id)); o SYNTAX o Table name No spaces, slashes, or periods o Fields Field name followed by a data type Data types next chapter Optional modifiers NULL, NOT NULL o Field must contain data PRIMARY KEY o Apply to table column(s) UNIQUE o Value in the field (not key) AUTO_INCREMENT o Access autonum o Numeric fields only ADD other tables to be used in examples o Courses Create table courses (course_num char(11) not null, Course_name varchar(30) not null, Course_room char(4) not null, PRIMARY KEY (course_num)); o Roster Create table roster (stu_id char (9) not null,
Course_num char(11) not null); o Assigns Create table assigns (assign_num int(3) not null auto_increment, assign_points int(3) not null, Assign_due date, primary key (assign_num)); Delete a table o DROP TABLE Show Tables; Drop table assigns; Show Tables; OOPS! Forgot to add field course in Students o ALTER TABLE Alter table students ADD course char(11); DESCRIBE Students; ALTER TABLE MODFIY To change name / data type/ qualifier of existing column ADD Records o INSERT COMMAND o Fields not specified automatically set to default values MUST include those specified as NOT NULL o With field names Order doesn t matter because column name is specified INSERT INTO students (stu_id, lname, fname, assign1, course) VALUES ( 123456789, Kelly, Michael,92, COMI1260100 ); o More common method INSERT INTO students VALUES ( 234567890, Kelly, Mary,null,83, COMI1260100 ); Proof SELECT * FROM Students; Maintaining Records o DELETE record from table ALL DELETE from students; Run Script to populate rest of data for use later Selected DELETE from students WHERE assign1 < 60;
o Change existing values (UPDATE) All columns UPDATE students SET class = PM ; Single column UPDATE students SET class = AM WHERE course = COMI1260001 ; Multiple columns Add columns following SET and separate by comma Query Samples o catch-all selection o Column specific SELECT course from students; Duplicates? SELECT DISTINCT course from students; o WHERE SELECT stu_id, lname, fname FROM students WHERE course = comi1260100 ; SELECT stu_id, lname, fname FROM students WHERE assign1 > 90; SELECT stu_id, lname, fname FROM students WHERE assign1 > 90 AND course = comi1260100 ; SELECT * FROM students WHERE course = comi1260001 OR comi1260100 ; Only need to specify column name if different o Functions SELECT stud_id, lname, fname, assign1, assign1 * 1.05 FROM students; SELECT stu_id, lname, fname, assign1, assign1 * 1.05 as adjusted_score FROM students; SELECT COUNT(*) FROM students;
SELECT MIN(assign1), MAX(assign1), AVG (assign1) FROM students; o Ordering and Limiting SELECT stud_id, lname, fname assign1 FROM students ORDER BY assign1; SELECT stud_id, lname, fname assign1 FROM students ORDER BY assign1 desc; SELECT * FROM students; SELECT * FROM students LIMIT 5, 3; Column to begin with Number of columns o Wildcards LIKE keyword % for 0-all occurrences _ position specific SELECT * FROM students WHERE course LIKE comi1260% ; SELECT * FROM students WHERE assign1 LIKE 8_ ; o Joining Tables Implements the relational aspect of RDBMS Referential integrity Key based Creates a data slice or subset SELECT stu_id, fname, lname, course, course_name, course_room FROM students, courses WHERE students.course = courses.course_num; o Using Table Alias SELECT stu_id, fname, lname, course, course_name, course_room FROM students s, courses c WHERE s.course = c.course_num; o 3 Tables? SELECT s.stu_id, fname, lname, course, course_name, course_room FROM students s, courses c, roster r WHERE r.course_num = c.course_num AND r.stu_id = s.stu_id; WHY DO WE HAVE course in students?
As Foreign Key To use in join later Referential integrity