使用日期列创建表

时间:2015-01-20 09:38:23

标签: mysql

我想创建一个学生表,其中包含“学生生日”栏目。其格式应为dd-mm-yy。

create table `student`.`studentinfo`(
    `student_id` int(10) not null auto_increment,
    `student_name` varchar(45) not null,
    `student_surname` varchar(45) not null,
    `student_birthday` date(???),
    (some lines of code)
primary key(student_id));

应该在(???)中输入什么来获得正确的格式?

3 个答案:

答案 0 :(得分:4)

只需使用不带括号的“DATE”。只有某些列类型需要括号,您希望指定可以存储的最大字节数/字符数。

对于MySQL,它记录在https://dev.mysql.com/doc/refman/5.6/en/date-and-time-types.html

答案 1 :(得分:3)

以下示例将解释您的问题。我正在使用 MySQL 5.7.18

首先我描述了用户表的结构,因为我要使用 FOREIGN KEY 创建帖子表。

稍后我创建了帖子表,它有一个 DATE 字段名为已创建,其中包含许多其他列。

最后我在新创建的表中插入了1行。

mysql> desc users;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| id          | bigint(20)  | NO   | PRI | NULL    |       |
| fname       | varchar(50) | NO   |     | NULL    |       |
| lname       | varchar(50) | NO   |     | NULL    |       |
| uname       | varchar(20) | NO   |     | NULL    |       |
| email       | text        | NO   |     | NULL    |       |
| contact     | bigint(12)  | NO   |     | NULL    |       |
| profile_pic | text        | NO   |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> CREATE TABLE  posts(id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, title text NOT NULL, description text NOT NULL, posted_by bigint, FOREIGN KEY(posted_by) REFERENCES users(id) ON DELETE CASCADE ON UPDATE RESTRICT , created DATE);
Query OK, 0 rows affected (0.01 sec)

mysql> desc posts;                                                                                                                                                          
+-------------+------------+------+-----+---------+----------------+
| Field       | Type       | Null | Key | Default | Extra          |
+-------------+------------+------+-----+---------+----------------+
| id          | bigint(20) | NO   | PRI | NULL    | auto_increment |
| title       | text       | NO   |     | NULL    |                |
| description | text       | NO   |     | NULL    |                |
| posted_by   | bigint(20) | YES  | MUL | NULL    |                |
| created     | date       | YES  |     | NULL    |                |
+-------------+------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> INSERT INTO posts(title, description, posted_by, created) values("Getting started with MySQL", "Excellent Database system", 1, "2017-05-26");
Query OK, 1 row affected (0.00 sec)

mysql> select * from posts;
+----+----------------------------+---------------------------+-----------+------------+
| id | title                      | description               | posted_by | created    |
+----+----------------------------+---------------------------+-----------+------------+
|  1 | Getting started with MySQL | Excellent Database system |         1 | 2017-05-26 |
+----+----------------------------+---------------------------+-----------+------------+
1 row in set (0.00 sec)

mysql> 

答案 2 :(得分:2)

数据类型date本身足以表示日期值。显示数据时格式很重要,您可以使用FORMAT列上的date功能。

我应该补充说,在插入日期时间文字时,格式有一定的灵活性,如文件here所示。