Learning Mysql


-> changetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)



Download 4.24 Mb.
View original pdf
Page176/366
Date04.08.2023
Size4.24 Mb.
#61806
1   ...   172   173   174   175   176   177   178   179   ...   366
Learning MySQL
-> changetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO mytime VALUES 1:2:3'),(3,NULL);
Query OK, 3 rows affected, 2 warnings (0.01 sec)
194 | Chapter 6:
Working with Database Structures

Records 3 Duplicates 0 Warnings 2
mysql> SELECT * FROM mytime;
+----+---------------------+
| id | changetime |
+----+---------------------+
| 1 | 0000-00-00 00:00:00 |
| 2 | 2006-07-16 01:02:03 |
| 3 | 2006-07-16 01:05:24 |
+----+---------------------+
3 rows inset sec)
Note how the current time is stored when we ask to insert a
NULL
value. Now, let’s change the id for the first row:
mysql> UPDATE mytime SET id WHERE id=1;
Query OK, 1 row affected (0.08 sec)
Rows matched 1 Changed 1 Warnings 0
mysql> SELECT * FROM mytime;
+----+---------------------+
| id | changetime |
+----+---------------------+
| 4 | 2006-07-16 01:05:42 |
| 2 | 2006-07-16 01:02:03 |
| 3 | 2006-07-16 01:05:24 |
+----+---------------------+
3 rows inset sec)
As you can seethe timestamp is updated to the current timestamp.
There are other variations on how you can control which column updates automatically, but if you stick to the previous steps, you’ll get the behavior you want. You can find more examples of using timestamps later in The Sample Music
Database.”
CHAR[(width)]
The most commonly used string type.
CHAR
stores a fixed-length string (such as a name, address, or city) of length
width
. If a
width
is not provided,
CHAR(1)
is assumed. The maximum value of
width
is 255. With MySQL versions between and 5.0.2, MySQL accepts values greater than 255 and silently changes the
CHAR
type to the smallest
TEXT
type that is suitable we discuss the
TEXT
type later in this section.
You can in fact define a special CHAR) NULL
column that takes up only one bit of storage. This provides two handy features. First, it allows you to include a dummy column in a table that doesn’t do anything (which might be useful as a placeholder fora future feature, or to be backward-compatible with an old application. Second, it allows you to store one of two values
NULL
or the empty string ''
, giving you very compact storage of binary (Boolean) values. To help you understand this better, let’s create a table with a
CHAR(0)
field, and an id field to help differentiate between entries:

Download 4.24 Mb.

Share with your friends:
1   ...   172   173   174   175   176   177   178   179   ...   366




The database is protected by copyright ©ininet.org 2024
send message

    Main page