SQL(1/7)

本文展示了如何在SQLite中通过字符串拼接操作将employees表的first_name和last_name字段合并为Name,以及如何在MySQL中创建actor表,包括其结构和初始数据的插入。此外,还演示了在actor表基础上创建actor_name表,只包含first_name和last_name字段。
摘要由CSDN通过智能技术生成

将employees表的所有员工的last_name和first_name拼接起来作为Name

将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
(注:sqllite,字符串拼接为 || 符号,不支持concat函数,mysql支持concat函数)
CREATE TABLE employees ( emp_no int(11) NOT NULL,
birth_date date NOT NULL,
first_name varchar(14) NOT NULL,
last_name varchar(16) NOT NULL,
gender char(1) NOT NULL,
hire_date date NOT NULL,
PRIMARY KEY (emp_no));

select last_name||' '||first_name as Name
from employees;

创建一个actor表

创建一个actor表,包含如下列信息
列表 类型 是否为NULL 含义
actor_id smallint(5) not null 主键id
first_name varchar(45) not null 名字
last_name varchar(45) not null 姓氏
last_update date not null 日期

create table if not exists arctor(
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'))
)

批量插入数据

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)

insert into actor
values('1','PENELOPE','GUINESS','2006-02-15 12:34:33'),
      ('2','NICK','WAHLBERG','2006-02-15 12:34:33')

批量插入数据

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);
insert into actor values (‘3’, ‘WD’, ‘GUINESS’, ‘2006-02-15 12:34:33’);

insert or ignore into actor
values(3,'ED','CHASE','2006-02-15 12:34:33');
# insert or ignore

创建一个actor_name表

对于如下表actor,其对应的数据为:
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2006-02-15 12:34:33
2 NICK WAHLBERG 2006-02-15 12:34:33

请你创建一个actor_name表,并且将actor表中的所有first_name以及last_name导入该表.
actor_name表结构如下:
列表 类型 是否为NULL 含义
first_name varchar(45) not null 名字
last_name varchar(45) not null 姓氏

create table actor_name
select first_name,last_name from actor;
# create table if not exists actor_name(
# first_name  varchar(45)  not null,
# last_name   varchar(45)  not null); -- 创建表

# insert into actor_name
# select     first_name,last_name
# from actor; -- 插入查询结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值