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.

Most RDBMSs handle the most common variations, like the preceding examples. However, the biggest<br />

problem arises when you specify the month by number instead of name — 03/12/2006, for example. An<br />

American would read this as the 12th day of March 2006, and a resident of the United Kingdom would<br />

see the date as the 3rd day of December 2006 — quite a difference! There is little else in database development<br />

that produces bigger headaches than dates!<br />

Even worse is when the computer the RDBMS is running is set to the American format and the computer<br />

accessing the data from the database is set to the U.K. format. And with many companies having<br />

U.S. and foreign-based offices, this can often happen.<br />

Whenever possible, avoid the number format, such as 12/07/2006, and instead use the month’s name or<br />

at least the abbreviation of its name (12 Jul 2006, for example). Unfortunately many RDBMSs return the<br />

date format as 12/07/2006 even if you stored it as 12 Jul 2006. In this case, use formatting commands,<br />

which are covered in Chapter 5, to circumvent this problem. Also try to make sure you know the format<br />

that the RDBMS server is using.<br />

Now that the brief introduction to data types is over with, you can move on to something a bit more<br />

interesting and hands-on — creating tables!<br />

Creating, Altering, and Deleting Tables<br />

This section discusses the basics of creating a table using <strong>SQL</strong>. It shows you how to create a new table,<br />

how to modify existing tables, and finally, how to delete tables that you no longer need. After that, using<br />

what you’ve learned, you create the tables for the book’s example database.<br />

Creating a Table<br />

To create a table, use <strong>SQL</strong>’s CREATE TABLE statement. Creating a basic table involves naming the table<br />

and defining its columns and each column’s data type.<br />

More advanced table options and constraints are covered in Chapter 4.<br />

The following is the basic syntax for creating a table:<br />

CREATE TABLE name_of_table<br />

(<br />

name_of_column column_datatype<br />

)<br />

Introduction to <strong>SQL</strong><br />

CREATE TABLE is the keyword telling the database system what you want to do — in this case, you want<br />

to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement.<br />

Then in brackets comes the list defining each column in the table and what sort of data type it is. The<br />

syntax becomes clearer with an example.<br />

25

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

Saved successfully!

Ooh no, something went wrong!