Ludzie pragną czasami się rozstawać, żeby móc tęsknić, czekać i cieszyć się z powrotem.
The PRIMARY KEY is actually an index that must contain unique values. It cannot be NULL. Every table should have a key, and MySQL allows you to easily create an index by declaring this key as your PRIMARY KEY.
Note
Even though MySQL does not require you to have them, keys are an essential
tool for database performance. Keys are covered extensively on Day 11, "MySQL
Table Locks and Assorted Keys."
The UNIQUE modifier enforces the rule that all data within the declared column must be unique. If you try to insert a value that is not unique, an error will be generated.
Summary
As you can see, there are many different kinds of data types. Each type has its own characteristics. It is up to you, the database designer, to associate the correct types with the type of data that you are going to store.
This is not always an easy job. But remember this, if you make a wrong choice, you can always change the type with an ALTER TABLE statement.
Today, you learned about all the various types. You learned about numeric types and their ranges, and you also learned about string types. You discovered that there were two groups of string types—
- 56 -
variable and fixed length. You learned the advantages and disadvantages of both of these. You also looked at the SET and ENUM types, and saw the difference between these two types and how they were better than ordinary string types. Finally, you read about some of the column modifiers and how they affect the column in which they are used.
Q&A
Q:
I want to sort the values of my
ENUM and SET columns. How do I do
this?
A:
The sort order depends on the order in which the values were inserted.
ENUM and SET types are not case sensitive. The value that is inserted reverts to the value that you used when you created the ENUM or SET. For example, if you declared an ENUM with the values "BLACK", "GREEN",
"RED", and inserted a row with the value red, it would be converted and stored as "RED".
Q:
I want to store images in my database. What is the best way to do this?
A:
There have been great debates in the discussion groups about this. One
argument against storing images or large files within MySQL is that MySQL
uses the file system to store its data. Why not let the file system also store
the image files and just store the path to the image? That way you don't
have to worry about fragmentation inside your database. The problem with
this is that if you are operating remotely, you must find some other way of
getting the file over to the remote system. There are both pros and cons to
both sides of this argument. I will cover how to do this both ways when we
get into Interfaces on Days 12, 13, 14 and 15.
Exercises
1. Using the knowledge you have gained here, go back and redefine all the column
values of the Meet_A_Geek project.
2. Practice inserting rows into a table using the SET and ENUM column types.
Week 1 in Review
At the end of this week, you should feel pretty comfortable working with the MySQL monitor. You also should have a firm understanding of database design. Additionally, you should know how to create a relational database with the proper data types.
Week 2: At a Glance
Day List
Day 8: Populating the Database
Day 9: Querying the Database
Day 10: Letting MySQL Do the Work—Intrinsic Functions
Day 11: MySQL Table Locks and Assorted Keys
Day 12: How to Get Data—Database Interface
Day 13: How to Use MyODBC
Day 14: The Perl Database interface
You should feel really comfortable working inside MySQL. There are no GUIs to learn—just simple commands. You should also have created a database. You will build on this sample database through each of these lessons.
Where You're Going
This week keeps up the fast pace by introducing you to the various ways to use MySQL. This week also builds on your first week by working with the database you created.
In this week's lessons, the focus is on how to use the database. The week will begin by teaching you how to populate your database with existing data or by transferring data from one database to another.
Day 9 covers the SQL programming language. In this lesson, you'll learn how to "talk" to your database.
Day 10, "Letting MySQL Do the Work—Intrinsic Functions," you'll learn about the functions that MySQL
- 57 -
can perform. Day 11 introduces you to MySQL table locks and assorted keys. The week ends with a series of chapters—Day 12, "How to Get to the Data—Database Interfaces," Day 13, "How to Use MyODBC," and Day 14, "The Perl Database Interface"—that deal with building programmatic interfaces in your MySQL database.
Again, a lot of information is going to be presented to you. Spend some time looking at the examples and doing the exercises and everything will become clear to you.
Day 8: Populating the Database
Overview
Now that you have designed and created your database, you're ready to put some data into it. You could enter all your data from the command line, but that could be a little tedious, not to mention a waste of time.