Arrays and SQL - Day 4

LeetCode

  • Title:  Maximum Subarray Sum

Given an integer array nums, please find a contiguous subarray with the largest sum (the subarray contains at least one element), and return its largest sum. A subarray  is a contiguous part of an array.

  •  code
	public static void main(String[] args) {
		int[] nums = { -1,-2};
		System.out.println(maxSubArray(nums));
	}

	public static int maxSubArray(int[] nums) {
		int pre = 0;
		int max = nums[0];
        for (int x : nums) {
            pre = Math.max(pre + x, x);
            max = Math.max(max, pre);
        }
        return max;
	}

nowcoder

  • Topic:  Add a new column named create_date after last_update

There is an actor table that contains the following information:

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);

Now add a new column named create_date after last_update, the type is datetime, NOT NULL, the default value is '2020-10-01 00:00:00'. Knowledge points

①Add field [ add at the end by default ]

ALTER TABLE <表名> ADD <字段名> <字段类型> [约束条件] 

② Add field [ Add at specified location ]

#在某个字段后面添加
ALTER TABLE <表名> ADD <字段名> <字段类型> [约束条件] AFTER <已存在的字段>
#在开头添加
ALTER TABLE <表名> ADD <字段名> <字段类型> [约束条件] FIRST

Set the default value when adding a field

ALTER TABLE <表名> ADD <字段名> <字段类型> [约束条件] DEFAULT <值>
  • code
ALTER TABLE actor ADD create_date datetime NOT NULL 
DEFAULT '2020-10-01 00:00:00' 
AFTER last_update

Guess you like

Origin blog.csdn.net/weixin_46899412/article/details/123609782