Simple SQL Peter Y. Dept of Computer and Information Systems Robert Morris University Simple SQL create table drop table insert into table values ( ) delete from table where update table set a to v where select * from table distinct all order by 2 (c) Peter Y - RMU. 1
Tables in SQL Query language for relational database Table = relation Column = attribute Row = tuple NOTE: SQL tables allow duplicate rows; will disallow duplicates only when a key constraint (primary key or secondary key) is violated. 3 Create Table To create a table, the simple SQL: Create table T1 (C1 number, C2 char(8), C3 varchar2(10)); Creates a table by the name T1, with 3 columns C1, C2, C3. C1takes numbers for data values. C2takes strings of 8 characters, padded with blanks. C3takes strings of up to 10 characters. The table T1 is then created, and the table has no data. Number of rows (records) in table T1 is zero, now. 4 (c) Peter Y - RMU. 2
Create Table create table T1 (C1 number, C2 char(8), C3 varchar2(10)); T1 C1 C2 C3 5 Create Table create table <table-name> (<column#1-name> <domain-name-for-column#1>, <column#2-name> <domain-name-for-column#2>, ); Domain-name can be any data type name, or a user-defined domain-name for some specific data type. Standard SQL has a set of data types for use; each vendor DBMS may have its own set. 6 (c) Peter Y - RMU. 3
create table: some data types for standard SQL, but every vendor system is different. char(n) fixed-length character string varchar(n) variable length character string number numerical value up to 40 spaces number(n) numerical value up to n spaces number(n,d) n spaces with d after decimal point integer same as number, integer values only integer(n) same as integer, but n spaces only date a date of the Gregorian Calendar: 28-Jul-2000 7 Drop Table To remove a table, drop it with SQL: drop table T1; The table T1 is removed (deleted). The table is gone. The data, if any in the table, is also gone. Can the drop table statement produce an error? 8 (c) Peter Y - RMU. 4
Insert data into a table Data in a table must be inserted one row at a time; to insert a row into a table insert into T1 (C1,C2,C3) values (33, apple, orange ); A row is inserted; note the corresponding values for each column. 9 Insert data into a table The values should match the column names. insert into T1 (C2,C3,C1) values ( peach, pineapple,7); A row is inserted; note the corresponding values for each column. 10 (c) Peter Y - RMU. 5
Look at the contents of a table To show the contents of a table: select * from T1; C1 C2 C3 ===== ======== ============ 33 apple orange 7 peach pineapple The content of table T1 is listed: all 3 columns, and all rows. 11 Look at the contents of a table To select just some of the columns to list select C2, C1 from T1; C2 C1 ========= ===== apple 33 peach 7 Only the selected columns are listed; those omitted are not listed. 12 (c) Peter Y - RMU. 6
SQL: insert 2 forms of SQL insert statement: insert into <table-name> [ (<column>,, <column>) ] values (<expr>,, <expr>); insert into <table-name> [ (<column>,, <column>) ] ( <select-statement> ); The second form of insert uses a sub-query the topic will be discussed again later, in more advanced SQL. 13 insert: a new record create table T1 (c1 integer, c2 integer); insert into T1 values (11,23); insert into T1 (c1,c2) values (22,45); insert into T1 (c2,c1) values (67,33); insert into T1 (c1) values (44); T1 C1 C2 11 23 22 45 33 67 44 14 (c) Peter Y - RMU. 7
insert: a whole table using sub-query create table T2 (N1 integer, N2 integer); insert into T2 (N2,N1) select c1,c2 from T1; T1 C1 C2 11 23 22 45 33 67 44 T2 N1 N2 23 11 45 22 67 33 44 15 SQL: delete Delete statement: delete from <table-name> [ where <search-condition> ]; Thewhere <search-condition> is optional. To delete all rows in the table T1: delete from T1; /* delete all rows in T1 */ The rows are deleted; the table becomes empty. The table is still there! 16 (c) Peter Y - RMU. 8
SQL: delete delete from T1 where c1 = 44; T1 T1 T1 C1 C2 C1 C2 C1 C2 11 23 11 23 22 45 22 45 33 67 33 67 44 delete from T1 where c2 = c1*2+1; Table T1 is now empty, but the table is still there. NOTE: delete may result in the update/deletion of rows in some other tables, to resolve referential integrity constraint violation. 17 SQL: update Update statement: update <table-name> [<tuple-variable>] set <column> = <expr>, <column> = <expr> where <serach-condition>; Update picks on the particular rows of the table which meet the search-condition, and updates the specified columns to the new values of the corresponding expressions. NOTE: update may result in the update/deletion of rows in some other tables, to resolve referential integrity constraint violation. 18 (c) Peter Y - RMU. 9
SQL: update update T2 set N1 = N2*2-1 where N2 = 44; T2 T2 T2 N1 N2 N1 N2 N1 N2 23 11 23 11 50 11 45 22 45 22 50 22 67 33 67 33 67 33 44 87 44 87 44 update T2 set N1 = 50 where N1 < 50; 19 SQL: select The basic SQL query has the form: Select C1,C2,,Cm from T1,T2,,Tn where <condition>; C1,C2,..Cm: column names (or expressions of) T1,T2,..Tn: table names simple query: use 1 table Example select instructor from course where topic = 4240; 20 (c) Peter Y - RMU. 10
SQL: select select instructor from course where CNum = 4240; INSTRUCTOR ---------- 21 SQL: select select title, enroll from course; TITLE ENROLL -------------------------------------- -------- Information Systems 25 Data Structures 30 Database Management 28 Java Programming 17 Electronic Commerce 20 22 (c) Peter Y - RMU. 11
SQL: select using expression select title, 30-Enroll as Available Seats from course; TITLE Available Seats -------------------------------------- --------------- Information Systems 5 Data Structures 0 Database Management 2 Java Programming 13 Electronic Commerce 10 23 SQL: select everything with * select * from course; CNUM SECTION TITLE INSTRUCTOR ENROLL ----- ------- --------------------- ---------- -------- 2240 A Data Structure 30 24 (c) Peter Y - RMU. 12
SQL: select another example select * from course where instructor = ; CNUM SECTION TITLE INSTRUCTOR ENROLL ----- ------- -------------------- ----------- -------- 25 What tables do I have? The information about your tables are also kept in a table called the system catalog. In Oracle it is abbreviated as cat select * from cat; TABLE_NAME TABLE_TYPE ================ =========== T1 TABLE Oracle SQL*Plus will then list the content of table cat, telling you what tables you have. 26 (c) Peter Y - RMU. 13
Duplicates and their Removal The result of an SQL query is also a table. Recall that the Relational Model does not allow duplicate rows in the same table, but DBMS supporting SQL allows that. The query result table may contain duplicates. How do the duplicate rows come about? For example, listing instructors in the course table select Instructor from Course where Enroll < 35; 27 Duplicates and their Removal To list the instructors only instructor names teaching as least one course with less than 35 students. select Instructor from course where Enroll < 35 ; Instructor Wood 28 (c) Peter Y - RMU. 14
To Remove Duplicates Use the keyword distinct immediately after the select keyword. select distinct Instructor from Course where Enroll < 35 ; Instructor Wood Removal of duplicate entries can be very very costly taking a lot of time (why?!); Caution: do that only when it is necessary! 29 Duplicates and their Removal The keyword all is optional: it is the default anyway no removal of duplicate entries unless keyword distinct is there. select all Instructor from Course where Enroll < 35 ; Instructor Wood 30 (c) Peter Y - RMU. 15
SQL: order by clause A table has NO pre-defined order. It is a set, therefore no order (& no duplicates). When we present the result of an SQL query as a table, we may specify order. Use the order by clause order by column [asc desc], The column may also be an expression. 31 SQL: order by clause select distinct Instructor from course where Enroll < 35; Find the instructors teaching courses with available seats. Instructor Wood Actually, there is NO guarantee about the order of the instructor names presented in the result of an SQL. 32 (c) Peter Y - RMU. 16
SQL: order by clause select distinct Instructor from course where Enroll < 35 order by instructor; -- List the names in alphabetical order. Instructor Wood The instructor names are sorted in the default ascending order when presented in the result. 33 SQL: order by clause select distinct instructor from course order by instructor desc; -- List the names in reverse alphabetical (descending) order. Instructor Wood The default is ascending order. 34 (c) Peter Y - RMU. 17
SQL: order by clause select distinct instructor, title from course order by instructor asc, title desc; Instructor Wood Title Electronic Comm Data Structures Information Syst Java Program Database Man Instructor names are in alphabetical order; Titles in reversed alphabetical order within each group identified by the instructor.. 35 (c) Peter Y - RMU. 18