Mysql 创建带有日期列的表

Mysql 创建带有日期列的表,mysql,Mysql,我想创建一个包含“student_Birth”列的student表,其格式应为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(?

我想创建一个包含“student_Birth”列的student表,其格式应为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));

在(??)中输入什么才能获得上面的正确格式?

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

我应该补充一点,在插入文档中的日期时间文本时,格式有一定的灵活性。

只需使用不带括号的“日期”。括号仅适用于某些列类型,其中需要指定可存储的最大字节数/字符数


对于MySQL,下面的示例将解释您的问题。我正在使用MySQL 5.7.18

首先,我描述了用户表的结构,因为我将使用外键创建posts表

后来我创建了posts表,它有一个名为created日期字段和许多其他列

最后,我在新创建的表中插入了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> 

是的。这:)一些数据类型可以有可选的附加属性(例如,
INT
UNSIGNED
,VARCHAR
的长度等),但是
Date
只是
Date
:)它工作得很好。实际上,我以前读过上面的文档,但是没有例子。