Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.

Similar presentations


Presentation on theme: "CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song."— Presentation transcript:

1 CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song

2 What is SQL SQL = Structured Query Language. SQL is a comprehensive database language. It has statements for data definition, query and update. SQL became a standard for relational databases in 1986, a revised and more extended version became a standard in 1992, A new version of the standard extends SQL with object-oriented and other recent database concepts. The SQL language provides a high-level declarative language interface: the user specifies what the result is to be and leaves the decisions on how to execute the query to the DBMS.

3 Basic SQL The terms table, row and column are used in SQL documents for relation, tuple and attribute respectively. The data types available for attribute values include numeric, character-string, bit-string, date and time. Numeric INTEGER FLOAT Character String CHARACTER(n) Bit String BIT(n) Date and Time DATE: yyyy-mm-dd TIME: hh:mm:ss

4 Create Table The CREATE TABLE command is used to specify a new relation, its attributes, their data type and constraints. The format is: CREATE TABLE tablename ( column name1 datatype1, column name2 datatype2, …… PRIMARY KEY (column name), FOREIGN KEY (column name) REFERENCES tablename (column name) ……..); To add the new records, use INSERT: The format is: INSERT INTO tablename VALUES (value1, value2….);

5 Create Table (example) To create a table for students, which records students’ name, student ID, sex, grade and birth date, and use student ID as the primary key. CREATE TABLE student (Name VARCHAR(10), ID CHAR(8), Sex CHAR(1), Grade CHAR(1), BDate DATE, PRIMARY KEY (ID)); To insert a student with name “John Smith”, ID number 123- 45-678, male, grade as B and born in Jan.1 st, 1988: INSERT INTO student VALUES (‘John Smith’, ‘12345678’, ‘M’, ‘B’, ‘1988-01-01’);

6 Basic Queries SQL has one basic statement for retrieving information from a database; the SELECT statement Basic form of the SQL SELECT statement (also called SELECT-FROM-WHERE block): SELECT FROM WHERE is a list of attribute names whose values are to be retrieved by the query is a list of the relation names required to process the query is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query So a SELECT-FROM-WHERE block is formed of the three clauses: SELECT, FROM, WHERE

7

8 Basic Queries (examples) A query gets involved with one relation. If the relation schema is: EMPLOYEE{Fname, Lname, Minit, Bdate, Address, DNo, SSN} Query: Retrieve the birth date and address of the employee whose name is ‘John B. Smith’. SQL: SELECT Bdate, Address FROM EMPLOYEE WHERE Fname=‘John’ AND Minit=‘B’ AND Lname=‘Smith’;

9 Basic Queries (examples) A query gets involved with two relation. Two relations schema are: EMPLOYEE{Fname, Lname, Minit, Bdate, Address, DNo, SSN} DEPARTMENT{Dname, Location, DNo} Query: Retrieve the name and address of all employees who work for the 'Research’ department. SQL: SELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname = ‘Research’ AND EMPLOYEE.DNo=DEPARTMENT.DNo;

10 Basic Queries (examples) Some queries need to refer to the same relation twice. If the given relation schema is EMPLOYEE{Fname, Lname, SSN, SUPERSSN} where the SUPERSSN is this employee’s supervisor’s SSN. Query: For each employee, retrieve the employee’s name, and the name of his or her immediate supervisor. SQL: SELECT E.Fname, E.Lname, S.Fname, S.Lname FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.SUPERSSN = S.SSN;

11 Basic Queries (examples) A missing WHERE clause indicates no condition, that is, all tuples of the relations in the FROM clause are selected. Query: Retrieve the SSN values of all the employees. SQL: SELECT SSN FROM EMPLOYEE


Download ppt "CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song."

Similar presentations


Ads by Google