Most columns are allowed to store NULL to indicate that the value is unknown. Say, someone's age or address. But sometimes you want to insist that a value is not allowed to be NULL, such as the Name or B-number or SSN.
CHAR
always takes up exactly the same amount of space, whether you use that space or not. It's a tiny, tiny bit faster than VARCHAR.
VARCHAR
saves space for shorter strings, at the price of being a tiny, tiny bit slower.
So, it's a space/speed tradeoff, where frugality usually wins.
Very much like arrays in Java, where you pre-allocate the space but if you don't use it all, it's wasted.
For both, the stated number is the upper
limit. But CHAR
always takes all of it,
while VARCHAR
might take up less space if the contents
are shorter.
Not as far as I know.
Very briefly. If we have an N-bit number, we can either use all N bits for "positive digits", with place values from 20 (= 1) to 2N-1. So, if N=4, the bits are 1, 2, 4 and 8, and we can create numbers from 0000 (= 0) to 1111 (= 15).
Alternatively, we can designate one of the bits as a sign bit, so half of our numbers become negative. So now with 4 bits we can only go up to 0111 (= 7) and down to -8.
ENUM syntax, and ENUM reading.
I specify the sort order in the definition of the column, not in the query.
Glad to. We omit the auto-increment column in the INSERT statement, or alternatively, leave it NULL.
The number is then generated when we insert.
I'll demo, using the staff
table in the cs304_db
.
Lots of times. Any time you are creating entities, you want them to have unique numbers, but you don't care what the numbers are.
ID values for Posts to Instagram, or to Stack Overflow, or, heck, even to the IMDB. Review IDs in Yelp or Amazon.com.
What a brilliant, amazing question! We will talk more about this later in the course when we talk about concurrency. It remembers the last id that you personnally inserted.
(Well, to be precise, the last insert ID in your connection to the MySQL server. If you exit and start again, it'll forget.)
Did the above help?
If you update one row, only that one row is affected (barring certain exotic things we are not talking about). So, 3 and 4 remain the same.
No. Updates are normal so they aren't considered exceptional. Also, we're supposed to know what we are doing. :-)
In CS 230, you learned a lot about data structures. Consider two: trees and hashtables. Most Relational Databases are stored using a kind of tree (called a B-tree). The leaves of the tree are different disk blocks, and disks allow random access. So if I'm looking up Ms Marvel in a database whose primary key is Name, I don't have to read any disk blocks other than the one with Ms Marvel on it. I don't have to read past Antman, Black Widow and Ironman...
They can also be stored as hashtables, whether the hash table tells us which disk block to read.
MySQL dump turns that data structure into a text file, allowing you to reconstruct the data structure if you need to, either as a backup, or to copy to another server, or just as a souvenir.
It's a nice idea, but not really. Databases are intended for high-speed updates, and saving the contents of the database on every update would defeat that that purpose.
That said, there are logging facilities that can be used to reconstruct the database from the last backup
Again, a good idea, but undercuts the goal of high speed. For our purposes in this class, we're going to insert, update and delete without keeping a record (though we could).
I think this is unlikely, because the primary key is used in ordering the data.
You could create a new table with the new primary key and copy things over, dropping the old table.
Yes, there is. We'll learn about alter table later in the course.
If you drop a table, the data is gone. You'd have to preserve the data in another table, or use a dump file.
Dump files are always a good safety net.
Be careful when dropping tables. There may be an "i-am-a-dummy" mode; I'm not sure.
Most tables will have a primary key, but not always.
Consider a table of people (uid,name,address,birthdate), including the Simpson family (1,"homer","472 Evergreen Terrace"). Now consider a table of hobbies: (uid,hobby), with entries like (1,"beer"), (1,"tv"), (1,"donuts").
The primary key would be the entire row, and its only usefulness would be in ensuring that we don't have any duplicates. Which is not nothing, but maybe not essential.