Docsity

Structured Query Language - Advance Java Web Technology - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Structured Query Language - Advance Java Web Technology - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

SQL

SQL

  • SQL is Structured Query Language
    • Some people pronounce SQL as “sequel”
    • Other people insist that only “ess-cue-ell” is the only correct pronunciation
  • SQL is a language for accessing and updating databases - SQL is an ANSI (American National Standards Institute) standard
  • Just about every relational database supports SQL
    • Most also extend it in various incompatible ways

Databases

  • A database contains one or more tables
    • Each table has a name
    • A table consists of rows and columns
    • A row is a record: it contains information about a single entity (such as a person)
    • Columns have names that tell what kind of information is stored in that column (for example, “Address”)
    • The information in a cell may be of various types: string, integer, floating point number, date, blank, etc. - A value of null means the data for that cell is missing - Two null values are not considered to be equal

Example table

  • People is the name of the table
  • Each row is a record
  • Each cell in a column contains the same kind of information
  • In this example, no single column contains unique information (there are two “John”s, etc.) - People
  • First_Name • Last_Name • Gender • Age • Phone
  • John • Smith • M • 27 • 2-
  • Sally • Jones • F • 27 • 3-
  • John • White • M • 32 • 2-
  • Mary • Smith • F • 42 • 5-

Integrity

  • Tables must follow certain integrity rules:
    • No two rows may be completely identical
    • Any column that is a primary key, or part of a primary key, cannot contain null values
    • There are some other rules about arrays and repeating groups that need not concern us here

DDL and DML

  • SQL consists of two kinds of “languages” (statement types) - DDL is the Data Definition Language; it defines the structure of tables - CREATE TABLE -- creates a new database table - ALTER TABLE -- alters (changes) a database table - DROP TABLE -- deletes a database table - DML is the Data Manipulation Language; it defines and manipulates the content of tables - INSERT -- puts new data into the database - SELECT -- gets data from the database - UPDATE -- updates (changes) data in the database - DELETE -- removes data from the database Docsity.com

Common data types

  • char( size )
    • Fixed-length character string (maximum of 255 characters)
  • varchar( size )
    • Variable-length character string (maximum of size characters)
  • number( size )
    • Integer value (max size digits)
  • number( size , d )
    • Decimal number value; maximum of size digits total, with not more than d digits to the right of the decimal
  • date
    • A calendar date

Example table creation

CREATE TABLE People ( First_Name VARCHAR(12), Last_Name VARCHAR(25), Gender CHAR(1), Age NUMBER(3), Phone CHAR(6) );

  • People
  • First_Name • Last_Name • Gender • Age • Phone
  • John • Smith • M • 27 • 2-
  • Sally • Jones • F • 27 • 3-
  • John • White • M • 32 • 2-
  • Mary • Smith • F • 42 • 5-

ALTER TABLE

  • ALTER TABLE table_name ADD column_name datatype - Adds a column to the table
  • ALTER TABLE table_name DROP COLUMN column_name - Removes a column (and all its data) from the table - DROP COLUMN is not available on all SQL platforms

DROP TABLE

  • Syntax: DROP TABLE table_name ;
  • Just deleting all the rows from a table leaves a “blank” table with column names and types
  • The DROP TABLE command removes the table from the database completely

How SELECT works

SELECT First_Name, Last_Name FROM People WHERE Age > 30;

  • People
  • First_Name • Last_Name • Gender • Age • Phone
  • John • Smith • M • 27 • 2-
  • Sally • Jones • F • 27 • 3-
  • John • White • M • 32 • 2-
  • Mary • Smith • F • 42 • 5-

John White Mary Smith

Result:

Names and strings

  • SQL keywords (such as SELECT) are case insensitive, but are traditionally written in all uppercase letters
  • Table names and column names may or may not be case sensitive
  • Data values presumably are case sensitive
  • String data must be enclosed in single quotes

Operators

  • Basic arithmetic operators are defined in SQL:
  • add
  • subtract
  • multiply / divide % modulus (remainder)

INSERT INTO

  • Syntax: INSERT INTO table_name ( column , …, column ) VALUES ( value , …, value );
  • The column s are the names of columns you are putting data into, and the value s are that data
  • String data must be enclosed in single quotes
  • Numbers are not quoted Docsity.com