Basic Database Tutorial

This Database Tutorial is written as a basic introduction to databases and database servers.

Server Types
There are several kinds of databases which include:
• • • •

Flat file databases Relational databases Object databases Object relational databases

Flat files are simply files with a table of information which may be separated by delimiters such as commas, colons, or semi-colons. Relational databases consist of several related tables of simple data. The tables are composed of rows and columns. Object databases store data in an object form rather than in tables. They store attributes and class information, but sometimes they also store and methods (behaviour) in the database. Object relational databases are relational databases with data stored in tables, but they have a front end that converts objects to data and data to objects, making it seem to the application that objects are being stored. Database servers include both a server program that serves remote clients and manages the database. They may use some means of standard communication between client and server to allow management of the data such as structured query language (SQL) for relational database servers. The most popular databases today are Relational Database Management Systems (RDBMS). However, object database servers may someday overtake the relational database servers.

Relational Databases and Objects
If relational databases are used to store objects, the object must first be disassembled into parts, normalized, and placed in tables. This can take some time to do, and be a labour intensive process required for writing the code. To use the object, it must be reassembled. Many relational databases are run on one single server and do not use a distributed architecture.

Object Database Servers
Object database servers may use an Object Query Language (OQL) as a standard language for communication. They may use an application programming interface (API) to allow the application to control the data or they may use both the API and OQL.

Current and Future Trends
Relational databases are still the most popular database in use today. There is good reason for this. They are easy to use and are normally efficient. However as programming has changed, tools related to those changes must also change. Object oriented programming is becoming much more popular and as that occurs a more practical tool for long term storage of data is in demand. This tool must interface easily to the object oriented language in question. It must also be a standard tool so users are not tied to specific vendors amd should have a standard way of exchanging information between applications and the database. OQL was developed for this purpose, but it does not appear to be widely supported yet by object oriented database vendors. Although object databases were first written many years ago, since they have not yet become popular, it appears that the market is not stable. There are several object oriented database vendors, and it is difficult to tell who will be in the market for the long haul. Therefore, I believe the purchase of an object oriented database is somewhat of a risk. This risk may be somewhat mitigated by the fact that programs can be written in object oriented language to isolate the programs from specific object database products. If the benefits of the object oriented database are large enough for the particular application they are used for and the specific organization considering them, the risks are likely to be worth taking. However, if the benefits are marginal, it may be worth waiting another year or two for more market stability and uniformity.

Database Basics: Part 1

The Primary Key simply refers to a column in your table that is guaranteed unique. This way you don't ever have to worry about its uniqueness since the database knows not to ever use the same number twice. each column in the table is given a unique name. then. an ID number of sorts. last_name. Also notice that the names don't use any spaces.By Joe Burns So. sort. link. The email address is much more likely to be unique but what if two people shared the same email? To avoid the uncertainty of using a data column as a primary key. Next. Spaces and symbols can be illegal characters that will mess up the whole works. how does the database keep things straight? Well. So what is the Primary Key in our example? Good question. many developers will create their own column which contains a computer generated unique How Do I Organize My Data? The next thing to understand about your table is the Primary paul@beatles. Obviously there are many people that share the same last name and/or first name that may be added to the database in the So. etc. first each database table is given a unique name. The Primary Key is then used for the purposes of indexing your table which makes it much more efficient to search. In our example. There is none. For example: John Paul Bill Smith McCartney Murray jsmith@huh. Without a unique name the DBMS (Database Management System) would get very confused. In our example above it would be something like first_name. Let's update our table now: Table name: contacts first_name John Paul Bill last_name Smith McCartney Murray email jsmith@huh. there is nothing that is going to be guaranteed unique. huh? Well. then the next step would be to learn about how the database works. . How Does My Data Get Stored? How the data is stored in a database is probably much simpler than you might gopher@caddyshack. A table simply refers to a two dimensional representation of your data using columns and rows. email. When naming tables and columns be sure to keep it simple with letters and numbers. Databases use a series of Tables to store the gopher@caddyshack. It only has to be unique within the table you have paul@beatles. This doesn't mean each column that you name has to be unique within the entire database. so if you need to clarify a name use the "_" instead of spaces. you've decided that you're interested in using a database in your web development.

Table Name: contact_newsletter_link contact_id 1 newsletter_id 2 . Let's say our contact table stores contact information for a subscription database for HTML Goodies. The best solution usually lies somewhere in the middle. Here's an example. How Does It All Relate? That's great. gopher@caddyshack. This would allow us to save the information we need but cause names and email addresses to be duplicated. I have the people in one table and the newsletters in another table. Table name: newsletters newsletter_id 1 2 name Goodies to Go Design Goodies description A newsletter for HTML fans. Hence the new and improved Relational Database. A newsletter for web & graphic designers.Table name: contacts contact_id 1 2 3 first_name John Paul Bill last_name Smith McCartney Murray email jsmith@huh. though. We could simply add another column in our contact table that would store the name of the newsletter. Now you will learn how to take those basic elements and make them relate to one another so that the information you are storing remains logically linked together. This way each newsletter name and description would be stored only once. we have a table of subscribers (contacts) and a table of newsletter information (newsletters). Relational Database? You never mentioned that. There is no limit to the number of tables. Now we need to store what newsletter(s) each person wants to subscribe to. Keep in mind. What about making a second table for the names of the newsletters. how do we know who subscribed to what? We make another table that links the two tables we already have together. So. that one huge table will be very inefficient while a bunch of little tables can be nearly impossible to keep paul@beatles. So. How the heck am I supposed to know who is subscribed to what? This is the best part. or columns for that matter. That would be highly inefficient. far you have learned some of the basic elements of a database. once for each different newsletter a person subscribes to. you can create. using our example above. This is where the Relational Database gets its How many tables should I use? That depends on how you can logically break down your data.

by referring to the table above you can see that John Smith has subscribed to Goodies to Go. Paul McCartney subscribed to Goodies to Go & Design Goodies and Bill Murray subscribed to Design Goodies. So. What is a Data Type? Well. LONG VARCHAR This will hold as many characters as you like up to 2 . You have to specify the maximum amount of characters that you will be putting here when you create your column. Amazing. In Part 2 of this series we will: • • • Learn about the most common data types Learn some basic SQL commands to create tables Learn how to create a database and tables with a tool like Access Database Basics: Part 2 Now that you have a feel for how to organize the contents of your database I'll bet you'd like to create some real tables. What's Next? Now that you have an understanding of the basic structure of a relational database we will examine the tables and the data a bit more closely. Data Types are pretty straight forward. A Foreign Key basically means that the number used in a Foreign Key column is not necessarily unique to the table it is in but is unique to the table it is referring to. huh? So. In this case contact_id is unique to the contact table but not necessarily unique to the contact_newsletter_link table. In this instance we are using Foreign Keys. Nothing is unique. what are the different Data Types? Here is a list of some of the most common ones and what type of values they hold: CHAR This will hold between 0 and 255 characters. Data Types. But what about that Primary Key thing you mentioned earlier? The ID numbers are used more than once. That's true. We now have the basics of a relational database. before we get started making a table there is one thing you need to understand first.2 2 3 1 2 1 This table then places a link between the contact table and the newsletters table using each tables unique ID number. They indicate the type of data that you are storing in a given table column.

more importantly. It is also referred to as a Boolean or Yes/No field. it's already installed on my machine. For our GUI example we are going to use Access since it is one of the most common databases out there today and. First the SQL Statement: CREATE TABLE contacts ( contact_id INT IDENTITY (1. It is primarily used for yes/no and true/false issues. Alright. CREATE TABLE makes sense. I know you're shocked. 1) NOT NULL . last_name CHAR (50) NULL . BIT This can hold a value of 0 or 1. You will also find that every DBMS has its own quirks and syntax. ☺ We'll use the example we started in Part 1. I'm going to show you 2 different ways to accomplish this task. email VARCHAR (75) NULL ).gigabytes of space. It is primarily used for mathematical purposes. You can store any whole number between -2147483648 and 2147483648. INT This type indicates that you are storing whole numbers here. That means the same Data Type can be referenced by different names in different DBMS's. This tells the DBMS to make a new table called "contacts". The first will be to use a SQL statement. FLOAT This type is used to store decimal numbers. . It is also commonly referred to as a TIMESTAMP since it is primarily used to Time Stamp entries or updates to a row or table. You'll have to check your DBMS documentation for specifics. DATE This stores a date. DATETIME This will store a date and time. there are a whole lot more but these will get you started and are among the most common. How Do I Create a Table? Creating a table is very easy. first_name CHAR (30) NULL . At first glance this statement can be a little bit intimidating but it really makes sense once you break it down. Granted. SMALLINT Same as above except you are limited to numbers between -32768 and 32768. The second will be using a Graphical User Interface (GUI) tool.

This means that this column must have a value and can never be empty. select the Create Table in Design View option in the middle window by double-clicking on it. From here we will be able to create all of the columns in your table. VARCHAR has one other important feature. First we have "contact_id". So. The creating of "last_name" is exactly like "first_name" except we are allowing up to 50 characters in this column. no matter how little information you put in them. That's it. Here we are stating that "first_name" is a character column (CHAR) that will store 30 or less characters in it. Now. Once you have selected the blank database option. Next is "first_name". . Essentially. so you will have to check your DBMS documentation to determine whether of not VARCHAR is going to be a benefit to you. Next we use IDENTITY which tells the DBMS that this is our Primary Key. With VARCHAR it would only take up as much space is needed for storage. "contact_id" will hold our unique index number that we call our Primary Key. Paul McCartney would have an ID of 2. If you remember. if you entered the name "John" in the "first_name" column. the (1. Lastly. There are many other DBMS's out there besides Access. Just to be different I used VARCHAR instead of CHAR. etc. To accomplish this. hence the VAR. Once Access has loaded the first order of business is to create a blank database. This means that when someone new is entered in the database the "first_name" and "last_name" columns will have a fixed size. 30 and 50 respectively. it can store a heck of a lot more than 255 characters if you want it to. in our example. So. John Smith would have an ID of 1.1) means that we want to start with the number 1 and we want to increment each new ID number by 1. we have "email". The real difference between CHAR and VARCHAR is the VARCHAR is variable. Lastly. storing a set of alphanumeric characters (a string). there is the statement NOT NULL. We want "contact_id" to be our primary key. Alright. The first column we will create is the "contact_id". it would take up a 30 character block of storage instead of only the 4 characters it really needs. Now that you have your database created. How Do I Design a Table in a Database Tool? Now we'll use some pretty pictures to illustrate the process of creating a database and table in Access. so we will need to let Access know that we want this field to be automatically numbered. That is what the NULL statement is for. This will send you to the design view window. This is a required element of any Primary Key. We are also saying the "first_name" can be empty if we want it to be.Now for the part in the parentheses. VARCHAR does come with a minimum storage requirement. you will need to give your database a name and a location to save it. first type "contact_id" in the first Field Name space. That means we know that column will always be a whole number so we make it an INT (Integer). it is the same as the CHAR in the statement and serves the same purpose. so it wasn't really easy but at least it is logical. so be sure to do a little homework and download some demos before you decide which DBMS is right for you.

Select that and it's done. Depending on the DBMS. This option tells Access whether you want this field to be a Long Integer or a Replication ID. As a matter of practice. You will see several menu choices including Primary Key. just one of the differences. tab over to the Data Type space. Again. We will choose Replication ID. It can be very handy in refreshing your memory when you or others come back to view a table. "last_name" and "email" in the next 3 Field Name spaces. First is the Field Size option. Text is the simplified Data Type that Access uses for anything that is to store some text like a name or email address. By choosing Replication ID. right-click on the Field Name that you want to be your key. in this case is "contact_id". you will likely see both of these references. You will notice there is no INT option or anything that refers to IDENTITY either. When selecting the Data Type for each one you will notice that there is no CHAR or VARCHAR available to you. This is an excellent example of how different DBMS's handle things differently. Be sure to set the Field Size length for each Text field equal to the limits we set in the SQL example above: first_name . In Access. AutoNumber is sort of an all-in-one thing. The only thing left is to set a Primary Key and save it. Next. Here is where you will declare this field's data type. It takes care of making the field an INT and an IDENTITY. . In this case you will select Text. The next 3 entries are all very straight forward. notice that there is a space for entering a description of the field that you are creating. which. the field will be automatically set to be used as an ID number which is exactly what we want. you should get used to entering a short description of what the field holds. A Long Integer will allow you some flexibility like determining whether the field is indexed (which makes it much more efficient at searches) or if the numbers are generated randomly or incrementally. To set your Primary Key. you will select AutoNumber for the Data Type. Lastly.75 Now you have a table that is almost done.30 last_name . however. You will notice that choosing Text gives you a different set of options in the bottom left of the window. There are a whole lot of options here but the only one we will be concerned with right now is the Field Size.50 email . Enter "first_name".Vocabulary Note: Field and Column are often used to refer to the same thing. You will notice that selecting AutoNumber gives you some different options in the bottom left section of the window.

how it works will probably need a little explanation. The trick is usually learning the individual syntax and the different procedures of each DBMS. "contacts". In Part 3 of this series we will: • • • Learn about the INSERT statement Learn about the UPDATE statement Learn how these statements relate to different DBMS's Database Basics: Part 3 [What's Next?] Alright. However. What about putting some data into the tables? What is an INSERT statement? I'm sure that it's not hard to guess what the INSERT statement does. and you're done. take some time to research different DBMS's and see what you think. . we're making some progress now. INTO and VALUES. Enter the name of the table. Like I stated earlier. What's Next? Now that you have a database and table to work with we'll take a look at how to get some data into it. You have a database and you've created some tables.To save your new table all you have to do is hit your close button in the top right of the window and you will be prompted to save your table if you want to. Each one is unique but the basics remain the same. In order to use INSERT you will need to learn about a couple of other important keywords.

Using the "contact" table that we created in part 2 we will begin to create an INSERT statement to enter data: INSERT INTO contacts This part of the statement tells the DBMS that we are about to insert a new row of data into the table "contacts". last_name. I'm sure you will be shocked to learn that VALUES is where you list what is to be inserted into your new row. Here is how you would insert that data: INSERT INTO contacts (first_name. . email) VALUES ('John'. Vocabulary Note: Row refers to a row of data in a table with each piece of data in its proper column. you could get an error if you try to do so. 'jsmith@huh. 'Smith'.Let's start with INTO. last_name. let's say for the sake of argument you are missing John's last name. Notice that the data is listed in the same order that we listed the column names above. What if you don't have anything to enter into a column? Maybe you are missing some data. last_name. In order for the DBMS to know where to put the data that you are adding it has to know what table you want the data to be added to. "last_name" and "email". "contact_id" is our column that automatically generates a new number each time a new row is created so there is no point in entering a number manually. how does the DBMS know what columns we want to insert data into? That's next: INSERT INTO contacts (first_name. For example: INSERT INTO contacts (first_name. Well. If you list your data in a different order you could end up with data in the wrong columns and create a major disaster for yourself. In fact. 'jsmith@huh. What about "contact_id"?').com'). email) This tells the DBMS that we are creating a row of data that will be placing information in the following columns: "first_name". So. The next keyword that you need to know is VALUES. NULL. email) VALUES ('John'.

let's update John's email address to jsmith@HTMLGoodies. here's the breakdown. By listing each column you spell out exactly what you are doing. . JSP and". not really. If you try to use a NULL in a column that must have a value in it you will get an error. Be careful.What is that NULL thing? NULL is how you tell the DBMS that you have nothing to enter in this column. gives you the background to fully utilize your database within whatever technology you choose. it's pretty obvious what UPDATE does. That's because the "contact_id" column is an integer (number) and not a string (group of characters). SET says we are going to change a row in the "email" column to "jsmith@HTMLGoodies. as usual. What is an UPDATE statement? That way if during some future update someone else was reading your code they could easily see what you had intended to do. The INSERT and UPDATE statements that you learned above along with many others will be necessary when you begin coding in technologies like ASP. Should I always list all of my table columns? Yes. There is much more to learning about databases but this will give you a good foundation to build on. Learning how to code SQL statements. however. what's the deal with the semicolon? The semicolon at the end of a statement simply tells the DBMS that you are done with that statement and it should process it now. UPDATE contacts SET email = 'jsmith@HTMLGoodies. Is that all there is to it? No. If you did try to put quotes around the 1 it would cause an error.) So. You can set as many columns as you like by separating each item with a comma. adding test data and examining how your SQL script effects a table. Now that you have some data in your table you may want to update it. especially if the table had been added to or changed over time. What you are learning here is merely the tip of the proverbial iceberg. There are GUI interfaces for almost all DBMSs now which pretty much layout your tables in an easy to use format so that you can enter data without typing a word of SQL. Isn't there a better way to enter some data? Better is relative. We know that John's ID number is 1. (We'll go through how you retrieve someone's ID number in Part 4. Notice the 1 does not have any single quotes around it. UPDATE tells the DBMS the we are going to change something in the table named "contacts". There is always more to learn and. So. though. let's update John's email address. WHERE then let's the DBMS know which row we want to change. These can be handy for doing some simple' WHERE contact_id = 1. Database FAQs So. DBMSs can very greatly when it comes to designating the end of a statement so be sure to check your DBMS and see which one is right for you. nothing stays the same. Alright. In this case we are changing the row where our ID number is 1.

You may remember quite vividly how you did your code from 2 weeks ago but will you have that clarity one year from now? Should I take advantage of shortcuts I find with my DBMS? That depends but probably not. Now. most DBMSs will simply stop and not execute the statement where the error occurred. how does it work? SELECT can range from very simple to painfully complex. three years later your business has outgrown your DBMS and you have to invest in an more robust DBMS. whenever you use a shortcut that is specific to your DBMS that you may sacrifice portability. So. you will need to specify what column(s) of data you will be retrieving. What's Next? Alright. For example. though. it tells the DBMS that you want to get some data out of the database. email This tells the DBMS that we are getting information only from the "first_name" and "email" columns of our table. calculate and arrange your data in an infinite number of ways. For example. What is a SELECT statement? So. if in the example above we forgot that "last_name" had to have a value and we tried to insert a NULL the INSERT statement would do absolutely nothing. . The next logical step is getting stuff back out. the data is in. Keep in mind. using our test table that we created say we want to get someone's email address and first name. In other words. Should I always spell out in painful detail everything that I am doing? Yes! It may be more typing in the short term but it sure makes life easier in the long run. Next. you have created a database for your web based business and took advantage of some shortcuts provided with your small business DBMS that you bought. what does SELECT do? Very simply. With SELECT there are a whole host of different options available to you that will allow you specify. If your DBMS has a shortcut that saves you some time typing it may be very tempting to use it. In this lesson you will learn about the most used statement in SQL. You begin to move your data and code over only to find that your shortcuts don't work with the new DBMS and you have to spend days or weeks finding all of the places that you used the shortcuts and fix them. It would not go ahead and create a row with just "John" in the "first_name" column.So. You would let the DBMS know what you are looking for by starting your SELECT statement like this: SELECT first_name. what happens if I generate a SQL error? Well. Now how do we get it back out? In Part 4 of this series we will: • • • Learn about the SELECT statement Learn about the FROM clause Learn about the WHERE clause Database Basics: Part 4 Now we have tables with stuff in them. Using a SELECT statement obviously begins with the SELECT command. the SELECT.

if you wanted to you could stop right here. our SELECT statement should look something like this now: SELECT first_name. If you want to get all of the columns out of any table you only need to use just one character. that's when the WHERE clause comes in to play. Now. Some developers have a tendency to use the asterisk almost exclusively even when they don't intend to use most of the information they are retrieving. email FROM contacts . like this: SELECT * The "*" tells the DBMS to send every column in the table.So. you can actually combine data from several different tables into one SELECT statement. What is a FROM clause? As you might have guessed. The next part of the SELECT statement is the FROM clause. Making use of the asterisk is very common but shouldn't be overused. it can be an incredible waste of system resources if it is abused. email FROM contacts Can I use more than one table in the FROM clause? Yes. What is a WHERE clause? A WHERE clause allows you to filter out any unwanted data so that the data you do get is exactly what you are looking for. While this may cut down on the amount of code you have to type in. However. But what if you wanted to get data for people with the first name of "Paul"? Well. this particular topic we are going to save for a bit later in the series. the FROM clause tells the DBMS what table to look in. SELECT first_name. Alright. what if I wanted to select all the columns in the table? Would I have to spell out each and every column in a table? Wouldn't that be a pain with a table that has a bunch of columns? Yes. Just add a semicolon to the end of the SELECT statement above and you would get every first name and email in the "contacts" table. This can be a more complex issue which I will explain later in the series along with selecting multiple column names from different tables. "*". Using our table we created in Part 2. what if I want to get stuff out of a few different tables? Do I have to make a SELECT statement for each table? No. you can. Let's look for all of the people named "Paul" in our table. it would be a pain and no you don't have to specify each column.

enter data and retrieve data. Operator = <> < <= > >= IS NULL Description Compares the data to your criteria to see if it is equal Compares the data to your criteria to see if it is not equal Compares the data to your criteria to see if the data is less than your criteria Compares the data to your criteria to see if the data is less than or equal to your criteria Compares the data to your criteria to see if the data is greater than your criteria Compares the data to your criteria to see if the data is greater than or equal to your criteria Checks the data to make sure that there is no data in the column What's Next? Now you have enough basic information to create a database. Keep in mind. create tables. though. Notice there are no single quotes around the number 3 this time.WHERE first_name = 'Paul'. There are a whole host of operators that you can use to make comparisons and filter data. each DBMS can be different and the operators they choose to use can change from DBMS to DBMS. In our case we will get only one result because there is only one person in our "contacts" table with the name of "Paul". Using the WHERE clause tells the DBMS you are only interested in data that has "Paul" in the "first_name" column. To search for the person that has the "contact_id" of 3 your SELECT statement would look like this: SELECT first_name. We can start to get organized. what if I want to filter my data using a number like in the "contact_id" column? Can I do that? Absolutely. not text. That's because we are filtering by a column that is numeric. Now is when it starts to get more interesting. Can I filter data in other ways? You bet. So. In Part 5 of this series we will: • • • Learn more about the WHERE clause Learn how to sort your data Learn how to group your data . Here are some of the most common operators that DBMSs use. email FROM contacts WHERE contact_id = 3.

You want to find the data on John Mellencamp but you can't remember how to spell his last name. (Just play along . They must have the first name of John but can have either the last name of Lennon or Mellencamp. In our scenario above this is how the wildcard would work: SELECT * . you're in luck. so be sure to check your DBMS before you start throwing wildcards around. How are you going to find him? Well. How about this scenario. Both AND and OR do exactly what you might think. different DBMS's use different characters for their wildcards. they allow you to string as many conditions together as you like. SQL has included several wildcards to help you out.Database Basics: Part 5 Alright. And just to make things more confusing. you can get your data back out now. Here's and example of both: SELECT * FROM table WHERE first_name='John' AND (last_name='Lennon' OR last_name='Mellencamp'). SQL gives you quite a lot of flexibility in defining how you filter your data. Imagine if you moved the parentheses around to look something like this: SELECT * FROM table WHERE (first_name='John' AND last_name='Lennon') OR last_name='Mellencamp'. What if you have a much more complex set of criteria that you would like to apply to filter? Well. grouping and narrowing down choices. though. Let's look at some ways to make your DBMS do some extra work for you by sorting. how does that work? It simply states that we are looking for anyone with the first name of John and the last name of Lennon or Mellencamp. So. Different wildcards tell the DBMS to do different things. Herman Mellencamp would be a valid name using this criteria but would not be valid in the first example. What if I have more than one criteria? You can use AND and OR. The parentheses not only help you to visually see how things are grouped together but they also let the DBMS know what to do..) You do remember that the last 4 characters are "camp".. Now what will the DBMS look for? In this case the DBMS will send back anyone named John Lennon or anyone with the last name of Mellencamp. Some more about WHERE In Part 4 you learned about using WHERE to filter your data.

it is very important where you place your wildcards.FROM table WHERE last_name LIKE '%camp'. What about Sorting? What if I want to sort my data? Do I have to write some customized sorting script to sort the data after I get it out? Thankfully. searching for a string (set of alphanumeric characters) is case sensitive. These wildcards are a bit more complex and beyond the scope of this lesson but we may come back to them later. can I use LIKE and = in the same WHERE clause? You sure can just as long as you don't try to combine them into a single comparison like last_name = LIKE '%camp'. So. It is also important that you don't overuse the wildcards. The wildcard tells the DBMS to look for anyone that has "camp" as the last 4 characters in their last name. Here's an example of how you would retrieve and sort all of the last names in our "contacts" table that we created way back in Parts 1 and 2: SELECT * FROM table ORDER BY last_name. Remember. Wildcards can easily get out of control if you overuse them. The % says everything before the last 4 characters is irrelevant. There are other wildcards as well like the brackets [] and the underscore _. Alright. If you are ever unsure of the case of something you may want to convert everything to upper or lower case before you make your comparisons. As you can see. SQL has already done that work for you. I'll show you how to do this later in this series. . It will return all of these examples: "camp". "campbell" and "mocamp". So. how about something like this now: WHERE last_name LIKE '%camp%' This will search for anyone with the characters "camp" anywhere in the last name. Hey! What's that LIKE doing there? The LIKE lets the DBMS know that we won't be doing a strict comparison like = or < or > but we will be using wildcards in our comparison. what if you moved that % after the "p"? WHERE last_name LIKE 'camp%' It will return all last names beginning with "camp" which probably will be none since we never capitalized the "c". though: WHERE first_name='John' AND last_name LIKE '%camp' Now. back to the wildcard itself. Something like this would be valid. no.

To search by more than one column you list the columns you want to be sorted in the order that you want them sorted. what if I wanted it to be sorted in reverse order from Z to A? That's easy too: SELECT * FROM table ORDER BY last_name DESC. COUNT(*) AS sales_by_company_total FROM sales GROUP BY company_name. COUNT keeps a running total of the number of records retrieved. you will usually put any count totals into a temporary column. Now. Let's take a look at COUNT first. You can use whatever name you like as your temporary column name.How's that for easy? The ORDER BY clause tells the DBMS to perform a sort on the data using the "last_name" column. As a general rule. This may not make much sense with this example but it will be more clear when we do the grouping in a minute. what you end up with here is the total number of all of the rows on the "sales" table which will be stored in the temporary column of "sales_total". Remember. and you want to make it abundantly clear to anyone reading your code what is going on you can use ASC in your ORDER BY clause like this: ORDER BY last_name ASC What if I want to sort by more than one column? Can it be done? Yep. Now the data that you get out will be pre-sorted by last name from A to Z. you are running a gift catalog business for corporations and you want to know how many items have been sold to each corporate client you have. "sales_total" is not a permanent part of your table but is rather a temporary column that you can refer to to get the count number from. So. . By default a DBMS will sort in ascending order and you don't really need to specify it. To accomplish this task you will need to learn about COUNT and GROUP BY. What about Grouping? Imagine this scenario. So. Here's an example of how all of that might work: SELECT COUNT(*) AS sales_total FROM sales. You will probably find yourself using ORDER BY frequently. first_name ASC Sorting in SQL is straight forward and very very very handy. If you wanted to sort by last name and then first name. In this case the DESC is used to tell the DBMS to sort by last name in descending order. let's do a little grouping: SELECT company_name. though. If you are like me. for example. it would look something like this: ORDER BY last_name.

effort and frustration. we will live dangerously and start deleting stuff. COUNT(*) AS sales_by_company_total FROM sales GROUP BY company_name ORDER BY company_name ASC. Grouping data can be almost as handy as sorting but you probably won't find as many occasions to use it. You will probably be surprised how much has already been done for you. Just a few more things and you'll be on your way to creating. Notice I changed "sales_total" to "sales_by_company_total" so that it would be a bit more descriptive. inserting. The COUNT then keeps a running total in the temporary column "sales_by_company_total" in accordance with the GROUP BY clause. In Part 6 of this series we will: • • • Learn about Aggregate Functions Learn about DISTINCT Learn how to delete your data Database Basics: Part 6 Now for some more functions that will help you get the data you want out of the database. before you try writing any custom scripting be sure to thoroughly check out all of the functions available with your DBMS.What this will do is search the "sales" table and count the total number of rows grouped by the company name which is what we Sears CompUSA Dollar General Stores So. updating and deleting data in your own database. What Are Aggregate Functions? . I also added the "company_name" column in the table to the SELECT statement so that our output will have both the company's name and total sales. Knowing how to use the built in functions of SQL can save you a great deal of time. The GROUP BY tells the DBMS to group everything by company name. what if I wanted to sort the grouped output? Not a problem. just add your ORDER BY clause at the end like this: SELECT company_name. The output would look something like this: company_name ---------------Amazon. So. sales_by_company_total ------------------------478 222 512 6 What's Next? You're almost there! So far. you have learned most of the necessary basics of SQL. And finally.

You can also use MAX and MIN to find dates. it adds together the all of the rows in a given column. These two functions are polar opposites of one another. Here is a portion of the SQL that I used to get the data I wanted: SELECT MIN(event_date) FROM event WHERE event_date > GETDATE(). the MIN and MAX functions. So. The result would be either the first or last row if the column were sorted alphabetically.U. Here's an example: . An example of this would be: SELECT MAX(sale_total) FROM daily_receipts WHERE sales_date = '01/01/2002' This example gets the MAXimum sale from the column "sale_total" in the table "sales_log" for sales made on 1/1/2002.Wouldn't it be great if SQL provided you with some basic functions for averaging. SUM does exactly what you would think. I'm sure you've guessed that aggregate functions do exactly that.." Anyway. Alright. For example. That value can be either a calculated value or a value from your data depending on the function. The home page highlights the next event scheduled and displays a description of the event. counting or finding the largest or smallest number in a set of numbers? Well. MAX return the highest value in a column while MIN returns the lowest value. Let's SUM it up now. This gives me the date of the event that is the closest to today's date that hasn't already past. Instead of just filtering your data. I just recently built a website that displays webcast seminar events and allows users to register. "that's 2/3 of a pun . For example. As my friend would say. P. aggregate functions actually return a value. bad pun but I couldn't resist. Here are some of the most used aggregate functions: Function MAX() MIN() SUM() AVG() COUNT() Description This returns a selected column's highest value This returns a selected column's lowest value This returns the sum of a selected column This returns the average value of a selected column This counts the number of rows in a selected column Let's take a look at each function and see how useful they can be. First.. Can I use MIN and MAX with a string of characters? Some DBMSs support using MIN and MAX on strings. when would you use MAX and MIN? You could use MAX and MIN for all sorts of mathematical purposes like finding the largest sale on a given day or which student had the lowest test score in a class. you may use SUM to total the sales for a given day in a retail store.

How would you go about this? If you do a SELECT on the "category" column of your table you will have a very long list of categories with lots of duplication. We'll use AVG on our example above to get the average sale for a given day: SELECT AVG(sales) FROM daily_receipts WHERE sales_date = '01/01/2002'. every DBMS is different and each one will have its own set of unique functions.SELECT SUM(sales) FROM daily_receipts WHERE sales_date = '01/01/2002'. however. Now you will have a complete list of categories with no duplicates because only unique values are returned. Why use DISTINCT with MIN or MAX when they are returning a single value anyway? . Here's an example of using COUNT to get the number of messages posted to a discussion group: SELECT COUNT(message) AS message_count FROM discussion_group WHERE category = 'SQL'. Then there's COUNT. Let's say that your discussion group has grown to over 20 categories. Instead of manually adding category names to all the forms and dropdown list boxes in your web by hand you decide it would be better to simply retrieve the information from the database and populate your forms automatically. You will probably find yourself using this function quite a bit. it doesn't make sense with some of the functions. You could use COUNT to find the total number of orders in a day. The administrator of your discussion group keeps adding categories right and left. consult your DBMS documentation. What's So Great About DISTINCT? We'll continue on with the discussion group scenario. Average is like a combination of SUM and COUNT with a little division thrown in. You should remember COUNT from Part 5. how many messages are posted to a discussion group and many other instances. For example. To accomplish your task you will want to use the DISTINCT clause. Then there's AVG. how many people signed up for class. if you are looking for something a little more specific than the basic functions we went through above. Here's how you would get the category list using DISTINCT: SELECT DISTINCT category FROM discussion_group. So. Average is about as straight forward as it gets. Like I've said before. Can I use DISTINCT with some of the aggregate functions above? You sure can.

If you forget the WHERE clause altogether you will probably find yourself scrambling to locate the last backup of your database. deleting data is all too simple. This will give you the total number of unique categories in your discussion group. For example. In this case DELETE remove the row in table "contacts" where the "contact_id" is equal to 3. what happens if I forget the WHERE clause? Well. being careful with DELETE cannot be stressed enough! In some cases. what if you wanted to count the number of categories in your discussion group? You might combine COUNT with DISTINCT like this: SELECT COUNT(DISTINCT category) FROM discussion_group. That means Bill Murray is history. What if you accidentally delete some very important data? What if you accidentally delete an entire table? These are all very real possibilities. So. DELETE may even be restricted by the Database Administrator to prevent the accidental deletion of data. that would be very bad news because you most likely just wiped out all of the data in the contacts table. Obviously. Unfortunately. How Do I Delete Stuff? Now for the scary part. Here is an example of how to delete a person from our sample database we created way back in Part 2: DELETE FROM contacts WHERE contact_id = 3. so take great care when issuing a DELETE to your database. In Part 7 of this series we will: • • Basic SQL Wrap-up Common Database Uses on the Web Database Basics: Part 7 . removing data from your database is necessary from time to time. The WHERE clause is so very important here. So. What's Next? Congratulations! You have the basics down now.With the other functions you may find a use for DISTINCT. If you don't get it exactly right you could end up deleting something you really wanted to keep. It can be a very scary thing. In the last part of this series we'll explore some great uses for databases on the web and try to wrap up any loose ends. though. deleting data.

our buying habits. You can download MySQL at MySQL. our personal contact information. MySQL is downloadable and free to individuals (be careful not to apply MySQL to commercial uses without a proper license). retrieveing and deleting data using SQL. Take some time and investigate the databases that are available to you. I'm sure you will come up with many more as you continue to develop your own personal websites. This tutorial should give you a good understanding of the fundamental concepts of adding. technical information and more. It may take you some time to find the one that will work for your system and get it implemented correctly. create a small project for yourself. A banking site allowing access to account If you are interested in continuing your education in SQL. It will also allow you to use some the SQL commands that you have learned here. They also provide us with information we need in the form of news content. though. We will also be doing an intermediate series on SQL in the future. our credit cards. you will probably notice that databases are used in almost everything we do. There are many more concepts and helpful tools just waiting for you out there. This will provide you with a perfect opportunity to practice your skills. updating. our taxes and more. Now let's look at some commercial web uses: • • A newsletter site that displays old articles stored in a database. so check HTMLGoodies often. I would suggest investigating MySQL. What Next? Once you have decided on a database. list of photographs of your children that your friends and family can download. Access will provide you with a straight forward interface to design and create tables. telephone numbers and email addresses. Anything Else? Remember. the skills that you have learned here are only the tip of the iceberg. We'll start off with some personal database projects: • • • • A A A A friends directory that can be accessed form your website. If you do not own a copy of Access and don't want to spend the money to get one. If you are looking something simple to start you off I would suggest Microsoft Access. MySQL comes in just under 100 different flavors for all sorts of operating systems (alright. A word of caution. what are some other uses for a database? If you take a moment to examine our information based world. DVD exchange that allows friends to post new DVD's they buy and loan movies out. 100 is a bit of an exaggeration). So.Congratulations! You've finished the Database Basics tutorial. Start with something simple like an address book that has your friends' names. there are several books available including Sams Teach Yourself SQL in 10 Minutes. Let's look at some specific examples that can be applied to the web. product information. schedule of events for your child's school activities that friends and family can access. . The next best step in learning how to work with databases is to actually create one yourself. addresses. Those are just a few suggestions. Information on almost anything you can think of is stored somewhere on a database. They track our checking accounts.

Good luck and happy programming! . Some of the more notable ones would include ASP. So. Perl. ASP. Each have their own advantages and disadvantages. A technical support site with technical information and downloads. there are many different technologies available to you that allow you to access all sorts of DBMS's.NET. how do I use a database with my web development? Well.• • • A scientific research site that provide white papers to researchers. PHP and JSP. Again. An online store of any kind. so take some time to check them out. And if none of the technologies I list here appeal to you there are many more out there. these are just a few of the instances where a database plays an iatrical role in providing information.