20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1<br />

<strong>SQL</strong> Server limits it to 123 characters. It’s safer to stick to letters, numbers, and the underscore character<br />

and to avoid any punctuation in the name. For example, My_db is fine, but £$%^my&&&db is unlikely to<br />

work or (and be honest) be easy to pronounce! Numbers are usually fine to include, but most RDBMSs<br />

don’t allow a database’s name to begin with a number. Finally, and it may seem obvious, a database<br />

name must be unique within the RDBMS. If you call two databases myDB, the RDBMS won’t know which<br />

one you’re referring to when you’re writing your <strong>SQL</strong> code.<br />

What if you want to delete the database? Again, most RDBMSs have a nice and easy user console that<br />

allows you do that, but you can also drop a database using <strong>SQL</strong>. You don’t use delete database as you<br />

might expect; instead you use the DROP DATABASE statement followed by the database’s name.<br />

So to drop the myFirstDatabase you write the following:<br />

DROP DATABASE myFirstDatabase<br />

This isn’t a command to be used lightly, though! Dropping the database removes it from the RDBMS and<br />

you could potentially lose all your data.<br />

Oracle is a bit of an exception when it comes to dropping databases. Instead of the DROP DATABASE<br />

command, you create the database again! If you already have a database called myFirstDatabase, the<br />

RDBMS deletes it if you write<br />

CREATE DATABASE myFirstDatabase<br />

This is something to be very careful of.<br />

After creating a database, the next stage is to add tables to it. However, before you can add tables, you<br />

need to look at the concept of data types.<br />

Understanding Data Types<br />

18<br />

Outside of the world of information technology, you categorize various bits of information into different<br />

types quite naturally. You think of the price of goods in a shop as being numerical data. If you ask for<br />

directions from New York to Washington, you expect to receive verbal instructions such as “turn left<br />

at....” In databases, a data type is the classification of different sorts of data being stored, whether the data<br />

are numbers, characters, or dates. It helps the database system make sense of the values being inserted<br />

into a database. So just as in the world outside databases, you categorize different types of data, but you<br />

do so in a more formal way. Returning to the train timetable example, the following table outlines what<br />

type of data each field holds:<br />

Field Data Type Example<br />

Start Character data London, Chester<br />

Destination Character data Manchester, Bristol<br />

Departs Time 10:15, 11:40<br />

Arrives Time 11:45, 18:00

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!