Presentation is loading. Please wait.

Presentation is loading. Please wait.

Indexes and more Table Creation

Similar presentations


Presentation on theme: "Indexes and more Table Creation"— Presentation transcript:

1 Indexes and more Table Creation

2 Indexes Increase database performance must be explicitly defined
once defined, are transparent to the user once created, the system maintains it more than one can exist on a given table The database is a piece of software that provides an interface to stored data. The interface we work with in a relational database is the table. This is a paradigm in that we work with it to directly manipulate data without every knowing the true format of the data.

3 Creating an Index Syntax Example CREATE [UNIQUE] INDEX index_name
ON table_name (column_name) Example create index auind on authors (au_id) Definition that will be used in this class

4 Composite Index Used when columns have a logical relationship and would be searched as a unit Example create index au_name_ind on authors (au_lname, au_fname) order not important, but performance is better when primary search col is first This illustrates these definitions

5 2 Kinds of Indexes Unique Index Clustered Index
The 4 main yntx operation we wil be performing in query and data manipulation. **Go through these

6 Unique Index No 2 rows are permitted to have the same value
system checks data upon creation and data addition rejects duplicates and returns an error should only be created on a column that requires uniqueness eg. ssn, acct code can be created as a composite or single column helps in maintaining data integrity boosts search performance Go over general select syntax. ** select and from are mutually inclusive This is probably one the most common statement you will be using. Used in reporting, etc.

7 Clustered Index System sorts rows on an ongoing basis so that the physical order is the same as the indexed order only 1 can exist per table should only be created for a column that is most often retrieved in order greatly increases performance when searching for contiguous key values… especially a range slows down data updates due to the sorting involved Some examples...

8 Things to Consider Indexes greatly increase query response time
every index requires system resources to store and maintain indexes can actually slow down the performance of UPDATES, INSERTS, and DELETES due to index maintenance So… don’t over index Insert in for inserting one row of data into a table

9 What Should We Index? Any column frequently used in retrieval
primary key columns columns that are often queried in a sorted order columns that are used in joins columns that are often searched for ranges UPDATE alters a records’ fields. Go over syntax

10 We Should NOT Index… Columns rarely used in queries
columns with 2 or 3 possible values eg. Male or Female small tables

11 SQL-92 Create Table Constraints
PRIMARY KEY rejects duplicates and nulls UNIQUE rejects duplicates, allows nulls DEFAULT inserts the default value when no value is entered CHECK validates data format FOREIGN KEY and REFERENCES ties foreign key to the primary key it references

12 Put it on paper! Codd’s 12 rules.
These are a summary of what we really need to know in this class. Everything is a table - any data present is only accessable thru a table. Data Independence - data is independent of the file format used by the DB Sublanguage - this is SQL Realational operations - these are the data maniplulation procedure we will be learning about (selectio, projection, join) View updating rule - view can be updated like a table null value - data is sometimes imperfect and a DB must be able to allow for this Integrety is always mainatined regardless of the structure

13 Then write your SQL create table title (title_id char(6) not null constraint tididx primary key constraint tidcheck check (title_id like ‘[A-Z] [A-Z] [0-9] [0-9]…’), title varchar(80) not null constraint titleidx unique, type char(12) default ‘unclassified’ null constraint typechk check (type in(‘business’, ‘mod_cook’, ‘trad_cook’)), pub_id char(4) null reference publishers (pub_id), price money null, advance money null)

14 Changing a Table Syntax
ALTER table table_name add column_name datatype null|not null The 2 kinds of tables User table contains the data you all will be loading. These are also the tables you will be creating. These will be the tables you query the most and will be modifying. System tables are maintained by the DB syste. They are not altered directly by ordinary users. Indirectly they are altered when tables are created. They can be queried like any other table. All tables are ranked equally in a relationl database. Permissions may vary.

15 Removing Objects Database Table Index DROP DATABASE db_name
deletes ALL tables and data within it!! Table DROP TABLE table_name deletes table and its contents Index DROP INDEX table_name.index_name deletes named index on named table

16 Joins What columns do I need? What tables have these columns?
Are all the tables related in some way? If not, are there other tables that can relate them? How are they all related? Link them together by setting their common fields equal in the WHERE clause. Restrict the WHERE clause to the record(s) of interest. UPDATE alters a records’ fields. Go over syntax

17 2 ways of looking at a Join
Looking at all the tables, linking them together and treating them like one big table. Setting the main search criteria and then linking the common fields to the data that is of interest.


Download ppt "Indexes and more Table Creation"

Similar presentations


Ads by Google