首页 > 试题广场 >

在last_update后面新增加一列名字为create_d

[编程题]在last_update后面新增加一列名字为create_d
  • 热度指数:125819 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
存在actor表,包含如下列信息:
CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')));
现在在last_update后面新增加一列名字为create_date, 类型为datetime, NOT NULL,默认值为'2020-10-01 00:00:00'
示例1

输入

drop table if exists actor;
CREATE TABLE  actor  (
   actor_id  smallint(5)  NOT NULL PRIMARY KEY,
   first_name  varchar(45) NOT NULL,
   last_name  varchar(45) NOT NULL,
   last_update  datetime NOT NULL);

输出

1
原来column后面不需要加括号
alter table actor
add column create_date datetime NOT NULL DEFAULT '2020-10-01 00:00:00'
after last_update
发表于 2024-07-18 19:27:42 回复(0)

加入'2020-10-01 00:00:00'大军

alter table actor add create_date datetime not null default '2020-10-01 00:00:00'
发表于 2024-06-05 20:25:08 回复(0)

在标准的日期和时间表示中,0000-00-00 00:00:00 这样的格式并不被视为有效的日期和时间值。具体原因如下:

**1. 不符合日期格式规范

按照常见的日期格式(如 ISO 8601),年份通常至少包含四位数字,月份和日期至少包含一位数字(两位更常见,如 YYYY-MM-DD)。小时、分钟和秒也是如此。因此,0000 作为年份、00 作为月份或日期,以及 00:00:00 作为时间部分,都不符合正常的日期和时间表示规则。

**2. 不具备实际意义

0000 作为年份无法对应任何真实的历史时期,因为公历纪元起始于公元1年。同样,00 作为月份和日期也无法对应实际的日期,因为一个月至少有一天,一天至少有一个小时、一分钟和一秒。因此,0000-00-00 00:00:00 无法代表任何一个具体的时刻。

**3. 数据库处理限制

在许多数据库系统(如 MySQL)中,使用 0000-00-00 00:00:00 作为日期和时间值会引发错误或警告。这是因为数据库通常会对日期和时间类型的数据进行严格的格式检查和有效性验证。例如,MySQL 5.7及以上版本默认开启了严格模式,不允许插入0000-00-00 作为 DATE 或 DATETIME 类型的值,除非特意关闭了相关的SQL模式(如 NO_ZERO_DATE)。

尽管如此,某些数据库或应用程序出于特定目的(如数据初始化、占位符、兼容旧数据等)可能会暂时容忍或使用类似 0000-00-00 00:00:00 的值。但在标准的日期和时间处理以及数据交换中,这样的格式是不被接受的。在遇到这类格式时,通常需要将其视为无效数据或特殊标记进行特殊处理。在设计和使用数据库时,应遵循最佳实践,避免使用无效日期,并确保数据完整性约束和验证规则能够防止此类值的插入。

编辑于 2024-04-18 23:00:03 回复(0)
alter table actor add column create_date datetime not null default '2020-10-01 00:00:00';

发表于 2024-02-06 20:18:39 回复(2)
alter table actor add column 
create_date datetime not null default '2020-10-01 00:00:00'
after last_update;

发表于 2022-12-12 22:00:04 回复(0)
ALTER TABLE actor
ADD
(create_date DATETIME NOT NULL DEFAULT '2020-10-01 00:00:00');

发表于 2022-09-06 10:13:26 回复(0)
alter table actor add column create_date datetime not null default '2020-10-01 00:00:00' after last_update
发表于 2022-08-05 13:32:32 回复(0)
alter table actor 
add 
column create_date datetime not null default'2020-10-01 00:00:00'
after last_update;
发表于 2022-06-10 21:53:28 回复(0)
alter table actor add column create_date datetime not null default '2020-10-01 00:00:00' after last_update;
发表于 2022-06-02 15:20:33 回复(0)
alter table actor add column create_date  datetime  not null default '2020-10-01 00:00:00'
after last_update

发表于 2022-04-18 11:36:49 回复(0)
alter table actor 
add create_date datetime NOT NULL default '2020-10-01 00:00:00'
after last_update;

发表于 2022-03-19 14:31:56 回复(0)
这一块的知识我一直记不住然后就去一直翻书我吐了
 alter table actor add column create_date datetime not null default "2020-10-01 00:00:00" after last_update


发表于 2022-03-11 22:05:19 回复(0)
ALTER TABLE actor 
    ADD COLUMN create_date DATETIME NOT NULL DEFAULT'2020-10-01 00:00:00';

发表于 2022-01-25 20:55:11 回复(0)
default 是指默认值
发表于 2021-12-29 13:06:10 回复(0)
BY 涛哥依旧@https://blog.csdn.net/stpeace/article/details/78145272

mysql命令什么时候带table关键字, 什么时候不带table关键字呢?
这是个简单的问题, 如果不注意, 每次不是多table就是少table, 心烦意乱。
我的经验是: 
       操作表本身的时候, 是需要带table的, 比如drop table tb_name;
       操作表的记录的时候, 是不需要带table的, 比如select * from tb_name;
发表于 2021-12-16 17:02:00 回复(0)
alter table actor 
add create_date 
datetime not null default '2020-10-01 00:00:00'
after `last_update`
/*ADD [COLUMN] 列明 列定义 [FIRST|AFTER 存在的列]*/

发表于 2021-11-19 15:14:50 回复(0)
alter table actor 
add COLUMN create_date datetime NOT NULL DEFAULT '2020-10-01 00:00:00' 
after last_update;
发表于 2021-11-17 15:56:52 回复(0)
ALTER TABLE actor ADD  column create_date datetime NOT NULL DEFAULT'2020-10-01 00:00:00';

发表于 2021-11-12 17:11:35 回复(0)