員工表結(jié)構(gòu):
1.將employees表的所有員工的last_name和first_name拼接起來作為Name采够,中間以一個空格區(qū)分
分析:MySQL連接可用concat()函數(shù)
select concat(last_name,' ',first_name) as name from employees;
2.創(chuàng)建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批量插入如下數(shù)據(jù):
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創(chuàng)建唯一索引uniq_idx_firstname,對last_name創(chuàng)建普通索引idx_lastname
在我的SQL常用操作(下)里有介紹創(chuàng)建索引的方法
CREATE UNIQE INDEX uniq_idx_firstname on actor(firse_name);
CREATE INDEX idx_lastname on actor(last_name);
5.針對actor表創(chuàng)建視圖actor_name_view,只包含first_name以及l(fā)ast_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表中,給出奇數(shù)行的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 ;