Download presentation
Presentation is loading. Please wait.
1
Database L. Grewe
2
Database Desired Features of a Database Management System (DBMS)
Easy to store, retrieve, modify data Security Handle Concurrent Access Data Recovery Consistent Data Metadata should be available.
3
History Flat-FileNot really a database, simply store data in files.
NetworkStores data in a network structure. HierarchicalStores in a hierarchical structure. Old, not used anymore RelationalStores data in tables. Makes use of the Math concepts of relations. Since 1970s. Example Oracle. Object-orientedExpands upon Relational model to handle more complicated kinds of data in non-transactional manners.
5
Accessing Database Administration
Many tools Some computer applications, some web interfaces Example of desktop application TOAD by Dell allows access to multiple databases Example of web interface --
6
Many databases
7
SQL or NoSQL SQL – structured query language -- used to access Relational databases
8
CAP theory Consistency Availability Partition tolerance
Says it is impossible for a distributed data store to simultaneously provide more than two out of the following three guarantees:[1][2][3] In other words, the CAP theorem states that in the presence of a network partition, one has to choose between consistency and availability. Consistency Availability Partition tolerance Every read receives the most recent write or an error Every request receives a (non-error) response – without guarantee that it contains the most recent write The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes
9
Which database SQL or NoSQL? Cost? Knowledge? Scaling? Features?
Availability?
10
Following trends Search, read, meetups
11
We are not talking about data in cloud or distributed data
Take CS 6320 to learn about these kinds of topics Your text here
12
Accessing Databases for Admin
Use DBA tool –desktop apps and web interfaces
13
Relational Databases Data is organized in tables. Each table consists of a set of columns, each column representing some item of information. A relation represents an operation on a database described by Mathematical Set Operations such as unions and joins. A Union B = all the elements of set A and B. A Joint B = only the comment elements of set A and B. For example, below is a table representing information about software distributors. They keys here are "Name, Street Address, City, State, Distribute" Search a database by searching against certain key values which represent For example, we may want to find all the entries in the Database that have the key State = CA. To search a database you must use the language the Database understands. Most relational databases understand a version of a scripting language called SQL. Name Street Address City State Distribute Butch Grewe 100 Campus Dr. Seaside CA Microsoft, Adobe Doug MacIntire 100 6th Street. NY Enterprise Suite
14
Relational Database Relational Databases Tablespace Table
Is a set of segments and consist of one or more data files. May contain many kinds of multiple data objects such as tables, indexes, etc. EXAMPLES in Oracle System tablespace = Contains the data dictionary, stored procedures, triggers, and system rollback segment. Used for system maintenance and operation. Data tablespace= Stores user data in tables and indexes. User data tablespaces don't need to be online all of th time unlike system tablespace. Need to be online in order to acces data in them. Note: There is often a defined default tablespace for each user. A user does not have to specify the tablespace when creating new objects, like tables, that they want to be in the default tablespace. The DBA sets up what the default tablespace is when creating a user. Temporary tablespace= Used by operations, such as sorting, unions, joins, that take up temporary space. Table Sits inside of a particular tablespace. Contains data in a table (row, column) format. When creating a table you can specify the tablespace it is to be inside of. If not the default tablespace for the user is assumed.
15
How to think about databases
TIP: Think of a database as a filing cabinent: the drawers withing the cabinet are tablespaces and the folders in those drawers are datafiles, and the pieces of paper in each folder are tables or other database objects, information written on a piece of paper is the data stored in the table.
16
Relational DB and SQL Structured Query Language = script language to manipulate a database
17
SQL: create table Using SQL*Plus to create a Table
below are two examples of creating tables. The second specifies the tablespace the table should appear in, used if you have more than one tablespace. It also specifies the initial size, how it grows in size and minimum number of extents allocated for it. SQL> create table customer( last_name varchar2(30) not null, state_cd varchar(2), sales number); SQL> create table products( name varchar2(30) not null, description varchar2(300), price number(4,2) ) tablespace productspace storage(initial 25k next 25k minextents 1); I prefer to use a GUI admin tool like TOAD or web interface to create table
18
SQL: Alter a table Using SQL*Plus to alter a Table
Use the SQL alter command to add or drop columns in a table after it has been created. Suppose we have a table customer that has the following fields: last_name, state_CD, sales To add a column tax_exempt_id SQL>alter table customer add tax_exempt_id varchar2(20); To drop the column sales SQL> alter table customer drop column sales;
19
CRUD – Create, Read, Update, Delete
SELECT select * from table select all the keys/fields from the table select Distribute from table select only the field Distribute from the table select * from table where Name is 'Butch Grewe' Select all keys/fields from table where the key/field Name = Butch Grewe
20
CRUD – Create, Read, Update, Delete
INSERT insert into table values ('James Bond', '1 Eiffel Tower', 'Paris', 'France','Spy Software') Insert into the table, the data entry having the following key/field entries in order: Name = James Bond Street Address = 1 Eiffel Tower City = Paris State = France (huh???) Distribute = Spy Software
21
CRUD – Create, Read, Update, Delete
update table set age=6 where Name='Butch Grewe' updates any records with the name 'Butch Grewe' to have the value of 6 for the field age.
22
CRUD – Create, Read, Update, Delete
delete from table where age=10 From table, deletes all records/rows that have the field of age with the value 10.
23
Conditional clauses The where clause where a1=x and a2=y
ALL of the conditions must be true where a1=x or a1=y Only one of the conditions need to be true where a1=x or a2 >z where a1 between x and y Range of values where a1 not between x and y NOT in range of values where a1 in (x, y, z) Must be in the list of items where a1 not in (x,y,z) Must NOT be in the list of items. where a1 != x Must be NOT equal to value. where a1 like 'M%' Pattern Search....means a1 must start with a capital M. where a1 like '%son%' Pattern Search....means a1 must have somewhere in its value the string 'son'. Examples that would be good: Furgeson, Forsonning, etc. where a1 not like 'M%' Pattern Search....means a1 does NOT start with a capital M.
24
Order clause the order by clause select * from table order by name
Order alphabetically in descending order the records in the table, by their value stored in the column name.
25
Oracle a relational database
Identification You identify an Oracle Database by the following information: Host IP = IP address where Oracle Database under consideration is installed. Port = Port number of Oracle Database installation. SID = ID of the particular tablespace that describes the database you wish to access. The following shows an example of some items that could be listed in this file: The first entry means the ( ) server under port number 1521, Oracle was installed with an SID = labdb to identify the table space under question and that TCP communications protocol should be used in accessing it. The second entry means that on the ( ) server under port number 1521, Oracle was installed with an SID = intprog to identify the tablespace labdb.world = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = ) (PORT = 1521)) (CONNECT_DATA = (SID = labdb)) ) INTERNETPROG.WORLD = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = ) (PORT = 1521)) (CONNECT_DATA = (SID = intprog)) )
26
Oracle data types Datatype Description Maximum Size CHAR
fixed-length cahracter field, paded with trailing blanks char(size) char(30) //30 characters 2000 bytes VARCHAR Currently, same as CHAR varchar(size) varchar(30) VARCHAR2 Variable length character field varchar2(size) 4000 bytes LONG Variable length character data 2GB NUMBER Variable length numeric data number OR number(l,d) where "l" stands for length and "d" for the number of decimal digits number(5,2) can store numbers like 1x10^-130 to 9.99x10^125 BOOLEAN holds values of true or false only. This is actually a PL/SQL data type. DATE Data time values represents using format DD-MON-YY (e.g. 09-SEP-0 which means September 9, 2004). Dec 31, 9999 RAW Variable length raw binary data raw(size) 20000 bytes LONG RAW Variable-length raw binary data 2 GB
27
Datatype Description Maximum Size VARRAY New to Oracle 8i, an array that is an ordered list. varray(num_elements) of type e.g. varray(100) of number // this is an array of maximum of 100 numbers. ROWID Row ID variable type 6 bytes User Defined Type User can define their own data type. You do so using the following SQL command. As a user, the DBA had to give you the privilege to be able to create user-defined data types. create type your_data_type_name as XXXX where XXX specifies the kind of info you want represented as your_data_type. Here are two examples: create type price_list as varray(100) of number; create type room_type as object( capacity number, podium boolean, computers number);
28
Oracle built in functions… go to oracle.com for more
Meaning ABS absolute value CEIL ceiling FLOOR the floor MOD modular POWER x to power of y ROUND round off the number SIGN retrieve the sign SQRT square root TRUNC truncate AVG take the average MAX the maximum of set MIN the minimum of set STDDEV calculate standard deviation SUM take the sum SYSDATE get the current sytem date
29
PHP and connecting to Oracle (or any similar relational database)
See api and some additional examples <?php $user="login_CHANGE_THIS"; $password= "password_CHANGE THIS"; $host = "mcsdb2.sci.csueastbay.edu:1521/MCSDB2"; //CHANGE THIS //setup sql query $query = "SELECT * FROM DOGS WHERE AGE > " echo "Query is = " .$query; echo "<br>"; echo "Host is = " .$host; echo "<br>"; echo "<br>"; //connect to database, where tsnames.ora is setup $connect_obj = oci_connect($user, $password, $host); if($connect_obj) { echo "connected okay"; } else { $err = oci_error(); echo "Oracle connection error " . $err['message']; return; } Setting up host and account information To connect to an Oracle instance SQL query to table DOGS to retrieve all Connect to Oracle and if any problems report error
30
This is about executing the SQL statement and catching any exceptions
Continued…… //create sql statement $sql_statement = oci_parse($connect_obj, $query) ; //execute statement try{ $r = oci_execute($sql_statement, OCI_COMMIT_ON_SUCCESS); if(!$r) { $p = oci_error($sql_statement); echo " <br> error in execution " . oci_error($p); } } catch(Exception $e) { echo "<br>Failed to get database info" . $e->getMessage(); } SQL statement object created using then oci php module Execute the SQL statement & results returned to $r If error report Catch any exceptions –like database not available, table doesn’t exist, etc
31
Free up the SQL statement and close connection – these are open resources and very important to free
Continued…… //close the connection oci_free_statement($sql_statement); //need to free so close can work //without this it will not close //until this script ends if(!oci_close($connect_obj)) {echo " oci connection not closed!!!"; } ?>
32
What does this php code do?
Very inefficiently (bad to do) makes a direct connection to data base, makes a single query and closes connection
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.