用日期列创建表

rea*_*ven 7 mysql

我想用“ student_birthday”列创建一个学生表,其格式应为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));
Run Code Online (Sandbox Code Playgroud)

在(???)中应该输入什么才能得到正确的格式?

neu*_*aus 5

只需使用不带括号的“ DATE”即可。仅对于要指定可存储的最大字节数/字符数的某些列类型,才需要使用方括号。

对于MySQL,请参见https://dev.mysql.com/doc/refman/5.6/en/date-and-time-types.html


hyg*_*ull 5

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

首先,我已经描述了users表的结构,因为我将使用FOREIGN KEY创建 posts 表。

后来我创建了posts表,它有一个名为createdDATE字段,其中包含许多其他列。

最后我在新创建的表中插入了 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> 
Run Code Online (Sandbox Code Playgroud)