员工表结构:
1.将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
分析:MySQL连接可用concat()函数
select concat(last_name,' ',first_name) as name from employees;
2.创建actor表,包含以下信息:
CREATE TABLE actor (
actor_id smallint(5) NOT NULL Primary key,
first_name varchar(45) NOT NULL,
last_name vachar(45) NOT NULL,
last_update timestamp NOT NULL DEFAUT (datetime('now','localtime'))
)
3.对于表actor批量插入如下数据:
INSERT INTO actor Values
(1,'PENELOPE','GUINESS',' 2006-02-15 12:34:33'),
(2, 'NICK', 'WAHLBERG', '2006-02-15 12:34:33');
4.对actor 表中的first_name创建唯一索引uniq_idx_firstname,对last_name创建普通索引idx_lastname
在我的SQL常用操作(下)里有介绍创建索引的方法
CREATE UNIQE INDEX uniq_idx_firstname on actor(firse_name);
CREATE INDEX idx_lastname on actor(last_name);
5.针对actor表创建视图actor_name_view,只包含first_name以及last_name两列,并对这两列重新命名,first_name为first_name_v,last_name修改为last_name_v
视图语法CREATE VIEW actor_name_view AS select first_name as first_name_v,last_name as last_name_v from actor;
6.actor表在last_update后面新增加一列名字为create_date, 类型为datetime, NOT NULL,默认值为'0000 00:00:00'
ALTER TABLE actor ADD COLUMN create_date datetime NOT NULL DEFAUT('0000-00-00 00:00:00');
7.获取employees中的first_name,查询按照first_name最后两个字母,按照升序进行排列
SELECT first_name FROM employees ORDER BY substr(first_name,-2);
8.对于employees表中,给出奇数行的first_name
SELECT e1.first_name from employees e1 where e
(select count(*) from employees e2 where e1.first_name>=e2.first_name) %2 = 1 ;
网友评论