SQL33 创建一个actor表,包含如下列信息
CREATE TABLE actor
(
actor_id smallint(5) not null comment'主键id',
first_name varchar(45) not null comment'名字',
last_name varchar(45) not null comment'姓氏',
last_update date not null comment'日期',
primary key(actor_id)
)
注:comment添加备注
SQL 34 批量插入数据
--虽然简单但是不安全,高度依赖表中列的定义顺序,尽量避免使用
INSERT INTO actor
VALUES(1,'PENELOPE','GUINESS','2006-02-15 12:34:33'),
(2,'NICK','WAHLBERG','2006-02-15 12:34:33')
--更繁琐,但更安全,表结构变化时也不会出错
insert into actor
(actor_id,first_name,last_name,last_update)
values(1,"PENELOPE","GUINESS","2006-02-15 12:34:33"),
(2,"NICK","WAHLBERG","2006-02-15 12:34:33")
题解1积累:批量插入数据(比如上百万)到MySQL中的多种操作_牛客博客
注:可省略列的条件(允许NULL值或已给出默认值)
SQL35 对于表actor插入如下数据,如果数据已经存在,请忽略(不支持使用replace操作)
题解:【Mysql】对于表actor插入如下数据,如果数据已经存在,请忽略(不支持使用replace操作)_牛客博客
-- mysql中常用的三种插入数据的语句:
-- insert into表示插入数据,数据库会检查主键,如果出现重复会报错;
-- replace into表示插入替换数据,需求表中有PrimaryKey,
-- 或者unique索引,如果数据库已经存在数据,则用新数据替换,如果没有数据效果则和insert into一样;
-- insert ignore表示,如果表中已经存在相同的记录,则忽略当前新数据;
insert ignore into actor values("3","ED","CHASE","2006-02-15 12:34:33");
SQL36 创建一个actor_name表,并且将actor表中的所有first_name以及last_name导入该表
CREATE TABLE actor_name
(
first_name varchar(45) not null comment'名字',
last_name varchar(45) not null comment'姓氏'
);
INSERT INTO actor_name
SELECT first_name,last_name FROM actor;
注:INSERT、SELECT插入查询结果