sql语句操作记录
发觉一些sql语句写出来的时候不停忘记,做一个记录。
mySQL
.查看表的创建过程sql语句和注释,注释是在创建表的过程中增加comment,后面跟随注释的内容
SHOW CRATE TABLE TABLENAME # 查看表创建过程
.查询表中的嵌套条件,父类包含子类的结果
SELECT * FROM test WHERE keyword="技术" OR paraent_id IN (SELECT DISTINCT id from 'test' WHERE keyword = "技术")
添加唯一索引或者唯一性约束的时候,字段中必须没有重复的值,不然会创建失败。MySQL不支持text类型的字段创建,会失败。
http://stackoverflow.com/questions/1827063/mysql-error-key-specification-without-a-key-length
.创建表时候给字段增加唯一约束
create table t3 (tid INT NOT NULL AUTO_INCREMENT,title VARCHAR(100)
NOT NULL UNIQUE,datetime DATE,user VARCHHAR(200),PRIMARY KEY(tid));
.给已经建立的表某个字段增加唯一约束
ALTER TABLE tablename add unique(fied)
.创建表时候给字段增加唯一索引
create table t2 (tid INT NOT NULL AUTO_INCREMENT,title VARCHAR(100)
NOT NULL,datetime DATE,user VARCHAR(200),PRIMARY KEY(tid),UNIQUE KEY u1 (user));
.给已经建立的表某个字段增加唯一索引
create unique index ti2 on t2(title);
.查看MySQL字符集
SHOW CHARACTER SET;
# 使用多列的主键成为复合主键
# 主键的值不允许被修改(实质可以修改)
# 字符串:CHAR(20) _ VARCHAR(20)
# 文本:TINYTEXT TEXT MEDIUMTEXT LONGTEXT
# 数值型:boolean,mediumint int bigint
float(p,s) double(p,s)
# 时间数据:date datetime timestamp year time
+++++ 完整的定义一张person表和favorite_food外键约束表流程 +++++
.创建一张person表,并且不给它增加主键自动增长
CREATE TABLE person (person_id SMALLINT UNSIGNED,name VARCHAR(20),gender ENUM('M','F'),
birth_date DATE,city VARCHAR(20),postal_code
VARCHAR(20), CONSTRAINT pk_person PRIMARY KEY (person_id));
.创建一张favorite_food,两个主键,外键引用person表的主键id
CREATE TABLE favorite_food (person_id SMALLINT UNSIGNED,
food VARCHAR(20),
CONSTRAINT pk_favorite_food PRIMARY KEY (person_id, food),
CONSTRAINT fk_fav_food_person_id FOREIGN KEY(person_id) REFERENCES person (person_id));
.现在给person表增加主键自增长,但是由于主键person_id被favorite_food引用,修改表增加自增长会失败,必须先删除掉favorite_food的外键引用
锁表:
LOCK TABLES favorite_food WRITE,person WRITE;
删除外键引用:
ALTER TABLE favorite_food DROP FOREIGN KEY
fk_fav_food_person_id;
.修改person表主键为自增长
ALTER TABLE person MODIFY person_id SMALLINT UNSIGNED AUTO_INCREMENT;
.增加favorite_food外键引用
ALTER TABLE favorite_food ADD CONSTRAINT fk_fav_food_person_id FOREIGN KEY (person_id) REFERENCES person (person_id) ON DELETE CASCADE;
.释放表:
UNLOCK TABLES;
+++++++ +++++++++++++
.子查询产生的表
SELECT e.emp_id,e.fname FROM (SELECT emp_id,fname,start_date,title FROM employee) AS e;
.创建视图
CREATE VIEW employee_vw AS SELECT emp_id,fname,YEAR(start_date) AS start_year FROM employee;
.使用视图
SELECT emp_id,start_year FROM employee_vw;
.表连接
SELECT e.emp_id,e.fname,department.name AS dept_name FROM employee as e inner join department ON e.dept_id = department.dept_id;
# 创建bank表项目
/* begin table creation */ create table department
(dept_id smallint unsigned not null auto_increment,
name varchar(20) not null,
constraint pk_department primary key (dept_id)
); create table branch
(branch_id smallint unsigned not null auto_increment,
name varchar(20) not null,
address varchar(30),
city varchar(20),
state varchar(2),
zip varchar(12),
constraint pk_branch primary key (branch_id)
); create table employee
(emp_id smallint unsigned not null auto_increment,
fname varchar(20) not null,
lname varchar(20) not null,
start_date date not null,
end_date date,
superior_emp_id smallint unsigned,
dept_id smallint unsigned,
title varchar(20),
assigned_branch_id smallint unsigned,
constraint fk_e_emp_id
foreign key (superior_emp_id) references employee (emp_id),
constraint fk_dept_id
foreign key (dept_id) references department (dept_id),
constraint fk_e_branch_id
foreign key (assigned_branch_id) references branch (branch_id),
constraint pk_employee primary key (emp_id)
); create table product_type
(product_type_cd varchar(10) not null,
name varchar(50) not null,
constraint pk_product_type primary key (product_type_cd)
); create table product
(product_cd varchar(10) not null,
name varchar(50) not null,
product_type_cd varchar(10) not null,
date_offered date,
date_retired date,
constraint fk_product_type_cd foreign key (product_type_cd)
references product_type (product_type_cd),
constraint pk_product primary key (product_cd)
); create table customer
(cust_id integer unsigned not null auto_increment,
fed_id varchar(12) not null,
cust_type_cd enum('I','B') not null,
address varchar(30),
city varchar(20),
state varchar(20),
postal_code varchar(10),
constraint pk_customer primary key (cust_id)
); create table individual
(cust_id integer unsigned not null,
fname varchar(30) not null,
lname varchar(30) not null,
birth_date date,
constraint fk_i_cust_id foreign key (cust_id)
references customer (cust_id),
constraint pk_individual primary key (cust_id)
); create table business
(cust_id integer unsigned not null,
name varchar(40) not null,
state_id varchar(10) not null,
incorp_date date,
constraint fk_b_cust_id foreign key (cust_id)
references customer (cust_id),
constraint pk_business primary key (cust_id)
); create table officer
(officer_id smallint unsigned not null auto_increment,
cust_id integer unsigned not null,
fname varchar(30) not null,
lname varchar(30) not null,
title varchar(20),
start_date date not null,
end_date date,
constraint fk_o_cust_id foreign key (cust_id)
references business (cust_id),
constraint pk_officer primary key (officer_id)
); create table account
(account_id integer unsigned not null auto_increment,
product_cd varchar(10) not null,
cust_id integer unsigned not null,
open_date date not null,
close_date date,
last_activity_date date,
status enum('ACTIVE','CLOSED','FROZEN'),
open_branch_id smallint unsigned,
open_emp_id smallint unsigned,
avail_balance float(10,2),
pending_balance float(10,2),
constraint fk_product_cd foreign key (product_cd)
references product (product_cd),
constraint fk_a_cust_id foreign key (cust_id)
references customer (cust_id),
constraint fk_a_branch_id foreign key (open_branch_id)
references branch (branch_id),
constraint fk_a_emp_id foreign key (open_emp_id)
references employee (emp_id),
constraint pk_account primary key (account_id)
); create table transaction
(txn_id integer unsigned not null auto_increment,
txn_date datetime not null,
account_id integer unsigned not null,
txn_type_cd enum('DBT','CDT'),
amount double(10,2) not null,
teller_emp_id smallint unsigned,
execution_branch_id smallint unsigned,
funds_avail_date datetime,
constraint fk_t_account_id foreign key (account_id)
references account (account_id),
constraint fk_teller_emp_id foreign key (teller_emp_id)
references employee (emp_id),
constraint fk_exec_branch_id foreign key (execution_branch_id)
references branch (branch_id),
constraint pk_transaction primary key (txn_id)
); /* end table creation */ /* begin data population */ /* department data */
insert into department (dept_id, name)
values (null, 'Operations');
insert into department (dept_id, name)
values (null, 'Loans');
insert into department (dept_id, name)
values (null, 'Administration'); /* branch data */
insert into branch (branch_id, name, address, city, state, zip)
values (null, 'Headquarters', '3882 Main St.', 'Waltham', 'MA', '');
insert into branch (branch_id, name, address, city, state, zip)
values (null, 'Woburn Branch', '422 Maple St.', 'Woburn', 'MA', '');
insert into branch (branch_id, name, address, city, state, zip)
values (null, 'Quincy Branch', '125 Presidential Way', 'Quincy', 'MA', '');
insert into branch (branch_id, name, address, city, state, zip)
values (null, 'So. NH Branch', '378 Maynard Ln.', 'Salem', 'NH', ''); /* employee data */
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Michael', 'Smith', '2001-06-22',
(select dept_id from department where name = 'Administration'),
'President',
(select branch_id from branch where name = 'Headquarters'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Susan', 'Barker', '2002-09-12',
(select dept_id from department where name = 'Administration'),
'Vice President',
(select branch_id from branch where name = 'Headquarters'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Robert', 'Tyler', '2000-02-09',
(select dept_id from department where name = 'Administration'),
'Treasurer',
(select branch_id from branch where name = 'Headquarters'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Susan', 'Hawthorne', '2002-04-24',
(select dept_id from department where name = 'Operations'),
'Operations Manager',
(select branch_id from branch where name = 'Headquarters'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'John', 'Gooding', '2003-11-14',
(select dept_id from department where name = 'Loans'),
'Loan Manager',
(select branch_id from branch where name = 'Headquarters'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Helen', 'Fleming', '2004-03-17',
(select dept_id from department where name = 'Operations'),
'Head Teller',
(select branch_id from branch where name = 'Headquarters'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Chris', 'Tucker', '2004-09-15',
(select dept_id from department where name = 'Operations'),
'Teller',
(select branch_id from branch where name = 'Headquarters'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Sarah', 'Parker', '2002-12-02',
(select dept_id from department where name = 'Operations'),
'Teller',
(select branch_id from branch where name = 'Headquarters'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Jane', 'Grossman', '2002-05-03',
(select dept_id from department where name = 'Operations'),
'Teller',
(select branch_id from branch where name = 'Headquarters'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Paula', 'Roberts', '2002-07-27',
(select dept_id from department where name = 'Operations'),
'Head Teller',
(select branch_id from branch where name = 'Woburn Branch'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Thomas', 'Ziegler', '2000-10-23',
(select dept_id from department where name = 'Operations'),
'Teller',
(select branch_id from branch where name = 'Woburn Branch'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Samantha', 'Jameson', '2003-01-08',
(select dept_id from department where name = 'Operations'),
'Teller',
(select branch_id from branch where name = 'Woburn Branch'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'John', 'Blake', '2000-05-11',
(select dept_id from department where name = 'Operations'),
'Head Teller',
(select branch_id from branch where name = 'Quincy Branch'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Cindy', 'Mason', '2002-08-09',
(select dept_id from department where name = 'Operations'),
'Teller',
(select branch_id from branch where name = 'Quincy Branch'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Frank', 'Portman', '2003-04-01',
(select dept_id from department where name = 'Operations'),
'Teller',
(select branch_id from branch where name = 'Quincy Branch'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Theresa', 'Markham', '2001-03-15',
(select dept_id from department where name = 'Operations'),
'Head Teller',
(select branch_id from branch where name = 'So. NH Branch'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Beth', 'Fowler', '2002-06-29',
(select dept_id from department where name = 'Operations'),
'Teller',
(select branch_id from branch where name = 'So. NH Branch'));
insert into employee (emp_id, fname, lname, start_date,
dept_id, title, assigned_branch_id)
values (null, 'Rick', 'Tulman', '2002-12-12',
(select dept_id from department where name = 'Operations'),
'Teller',
(select branch_id from branch where name = 'So. NH Branch')); /* create data for self-referencing foreign key 'superior_emp_id' */
create temporary table emp_tmp as
select emp_id, fname, lname from employee; update employee set superior_emp_id =
(select emp_id from emp_tmp where lname = 'Smith' and fname = 'Michael')
where ((lname = 'Barker' and fname = 'Susan')
or (lname = 'Tyler' and fname = 'Robert'));
update employee set superior_emp_id =
(select emp_id from emp_tmp where lname = 'Tyler' and fname = 'Robert')
where lname = 'Hawthorne' and fname = 'Susan';
update employee set superior_emp_id =
(select emp_id from emp_tmp where lname = 'Hawthorne' and fname = 'Susan')
where ((lname = 'Gooding' and fname = 'John')
or (lname = 'Fleming' and fname = 'Helen')
or (lname = 'Roberts' and fname = 'Paula')
or (lname = 'Blake' and fname = 'John')
or (lname = 'Markham' and fname = 'Theresa'));
update employee set superior_emp_id =
(select emp_id from emp_tmp where lname = 'Fleming' and fname = 'Helen')
where ((lname = 'Tucker' and fname = 'Chris')
or (lname = 'Parker' and fname = 'Sarah')
or (lname = 'Grossman' and fname = 'Jane'));
update employee set superior_emp_id =
(select emp_id from emp_tmp where lname = 'Roberts' and fname = 'Paula')
where ((lname = 'Ziegler' and fname = 'Thomas')
or (lname = 'Jameson' and fname = 'Samantha'));
update employee set superior_emp_id =
(select emp_id from emp_tmp where lname = 'Blake' and fname = 'John')
where ((lname = 'Mason' and fname = 'Cindy')
or (lname = 'Portman' and fname = 'Frank'));
update employee set superior_emp_id =
(select emp_id from emp_tmp where lname = 'Markham' and fname = 'Theresa')
where ((lname = 'Fowler' and fname = 'Beth')
or (lname = 'Tulman' and fname = 'Rick')); drop table emp_tmp; /* product type data */
insert into product_type (product_type_cd, name)
values ('ACCOUNT','Customer Accounts');
insert into product_type (product_type_cd, name)
values ('LOAN','Individual and Business Loans');
insert into product_type (product_type_cd, name)
values ('INSURANCE','Insurance Offerings'); /* product data */
insert into product (product_cd, name, product_type_cd, date_offered)
values ('CHK','checking account','ACCOUNT','2000-01-01');
insert into product (product_cd, name, product_type_cd, date_offered)
values ('SAV','savings account','ACCOUNT','2000-01-01');
insert into product (product_cd, name, product_type_cd, date_offered)
values ('MM','money market account','ACCOUNT','2000-01-01');
insert into product (product_cd, name, product_type_cd, date_offered)
values ('CD','certificate of deposit','ACCOUNT','2000-01-01');
insert into product (product_cd, name, product_type_cd, date_offered)
values ('MRT','home mortgage','LOAN','2000-01-01');
insert into product (product_cd, name, product_type_cd, date_offered)
values ('AUT','auto loan','LOAN','2000-01-01');
insert into product (product_cd, name, product_type_cd, date_offered)
values ('BUS','business line of credit','LOAN','2000-01-01');
insert into product (product_cd, name, product_type_cd, date_offered)
values ('SBL','small business loan','LOAN','2000-01-01'); /* residential customer data */
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '111-11-1111', 'I', '47 Mockingbird Ln', 'Lynnfield', 'MA', '');
insert into individual (cust_id, fname, lname, birth_date)
select cust_id, 'James', 'Hadley', '1972-04-22' from customer
where fed_id = '111-11-1111';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '222-22-2222', 'I', '372 Clearwater Blvd', 'Woburn', 'MA', '');
insert into individual (cust_id, fname, lname, birth_date)
select cust_id, 'Susan', 'Tingley', '1968-08-15' from customer
where fed_id = '222-22-2222';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '333-33-3333', 'I', '18 Jessup Rd', 'Quincy', 'MA', '');
insert into individual (cust_id, fname, lname, birth_date)
select cust_id, 'Frank', 'Tucker', '1958-02-06' from customer
where fed_id = '333-33-3333';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '444-44-4444', 'I', '12 Buchanan Ln', 'Waltham', 'MA', '');
insert into individual (cust_id, fname, lname, birth_date)
select cust_id, 'John', 'Hayward', '1966-12-22' from customer
where fed_id = '444-44-4444';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '555-55-5555', 'I', '2341 Main St', 'Salem', 'NH', '');
insert into individual (cust_id, fname, lname, birth_date)
select cust_id, 'Charles', 'Frasier', '1971-08-25' from customer
where fed_id = '555-55-5555';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '666-66-6666', 'I', '12 Blaylock Ln', 'Waltham', 'MA', '');
insert into individual (cust_id, fname, lname, birth_date)
select cust_id, 'John', 'Spencer', '1962-09-14' from customer
where fed_id = '666-66-6666';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '777-77-7777', 'I', '29 Admiral Ln', 'Wilmington', 'MA', '');
insert into individual (cust_id, fname, lname, birth_date)
select cust_id, 'Margaret', 'Young', '1947-03-19' from customer
where fed_id = '777-77-7777';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '888-88-8888', 'I', '472 Freedom Rd', 'Salem', 'NH', '');
insert into individual (cust_id, fname, lname, birth_date)
select cust_id, 'Louis', 'Blake', '1977-07-01' from customer
where fed_id = '888-88-8888';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '999-99-9999', 'I', '29 Maple St', 'Newton', 'MA', '');
insert into individual (cust_id, fname, lname, birth_date)
select cust_id, 'Richard', 'Farley', '1968-06-16' from customer
where fed_id = '999-99-9999'; /* corporate customer data */
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '04-1111111', 'B', '7 Industrial Way', 'Salem', 'NH', '');
insert into business (cust_id, name, state_id, incorp_date)
select cust_id, 'Chilton Engineering', '12-345-678', '1995-05-01' from customer
where fed_id = '04-1111111';
insert into officer (officer_id, cust_id, fname, lname,
title, start_date)
select null, cust_id, 'John', 'Chilton', 'President', '1995-05-01'
from customer
where fed_id = '04-1111111';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '04-2222222', 'B', '287A Corporate Ave', 'Wilmington', 'MA', '');
insert into business (cust_id, name, state_id, incorp_date)
select cust_id, 'Northeast Cooling Inc.', '23-456-789', '2001-01-01' from customer
where fed_id = '04-2222222';
insert into officer (officer_id, cust_id, fname, lname,
title, start_date)
select null, cust_id, 'Paul', 'Hardy', 'President', '2001-01-01'
from customer
where fed_id = '04-2222222';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '04-3333333', 'B', '789 Main St', 'Salem', 'NH', '');
insert into business (cust_id, name, state_id, incorp_date)
select cust_id, 'Superior Auto Body', '34-567-890', '2002-06-30' from customer
where fed_id = '04-3333333';
insert into officer (officer_id, cust_id, fname, lname,
title, start_date)
select null, cust_id, 'Carl', 'Lutz', 'President', '2002-06-30'
from customer
where fed_id = '04-3333333';
insert into customer (cust_id, fed_id, cust_type_cd,
address, city, state, postal_code)
values (null, '04-4444444', 'B', '4772 Presidential Way', 'Quincy', 'MA', '');
insert into business (cust_id, name, state_id, incorp_date)
select cust_id, 'AAA Insurance Inc.', '45-678-901', '1999-05-01' from customer
where fed_id = '04-4444444';
insert into officer (officer_id, cust_id, fname, lname,
title, start_date)
select null, cust_id, 'Stanley', 'Cheswick', 'President', '1999-05-01'
from customer
where fed_id = '04-4444444'; /* residential account data */
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Woburn' limit 1) e
cross join
(select 'CHK' prod_cd, '2000-01-15' open_date, '2005-01-04' last_date,
1057.75 avail, 1057.75 pend union all
select 'SAV' prod_cd, '2000-01-15' open_date, '2004-12-19' last_date,
500.00 avail, 500.00 pend union all
select 'CD' prod_cd, '2004-06-30' open_date, '2004-06-30' last_date,
3000.00 avail, 3000.00 pend) a
where c.fed_id = '111-11-1111';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Woburn' limit 1) e
cross join
(select 'CHK' prod_cd, '2001-03-12' open_date, '2004-12-27' last_date,
2258.02 avail, 2258.02 pend union all
select 'SAV' prod_cd, '2001-03-12' open_date, '2004-12-11' last_date,
200.00 avail, 200.00 pend) a
where c.fed_id = '222-22-2222';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Quincy' limit 1) e
cross join
(select 'CHK' prod_cd, '2002-11-23' open_date, '2004-11-30' last_date,
1057.75 avail, 1057.75 pend union all
select 'MM' prod_cd, '2002-12-15' open_date, '2004-12-05' last_date,
2212.50 avail, 2212.50 pend) a
where c.fed_id = '333-33-3333';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Waltham' limit 1) e
cross join
(select 'CHK' prod_cd, '2003-09-12' open_date, '2005-01-03' last_date,
534.12 avail, 534.12 pend union all
select 'SAV' prod_cd, '2000-01-15' open_date, '2004-10-24' last_date,
767.77 avail, 767.77 pend union all
select 'MM' prod_cd, '2004-09-30' open_date, '2004-11-11' last_date,
5487.09 avail, 5487.09 pend) a
where c.fed_id = '444-44-4444';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Salem' limit 1) e
cross join
(select 'CHK' prod_cd, '2004-01-27' open_date, '2005-01-05' last_date,
2237.97 avail, 2897.97 pend) a
where c.fed_id = '555-55-5555';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Waltham' limit 1) e
cross join
(select 'CHK' prod_cd, '2002-08-24' open_date, '2004-11-29' last_date,
122.37 avail, 122.37 pend union all
select 'CD' prod_cd, '2004-12-28' open_date, '2004-12-28' last_date,
10000.00 avail, 10000.00 pend) a
where c.fed_id = '666-66-6666';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Woburn' limit 1) e
cross join
(select 'CD' prod_cd, '2004-01-12' open_date, '2004-01-12' last_date,
5000.00 avail, 5000.00 pend) a
where c.fed_id = '777-77-7777';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Salem' limit 1) e
cross join
(select 'CHK' prod_cd, '2001-05-23' open_date, '2005-01-03' last_date,
3487.19 avail, 3487.19 pend union all
select 'SAV' prod_cd, '2001-05-23' open_date, '2004-10-12' last_date,
387.99 avail, 387.99 pend) a
where c.fed_id = '888-88-8888';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Waltham' limit 1) e
cross join
(select 'CHK' prod_cd, '2003-07-30' open_date, '2004-12-15' last_date,
125.67 avail, 125.67 pend union all
select 'MM' prod_cd, '2004-10-28' open_date, '2004-10-28' last_date,
9345.55 avail, 9845.55 pend union all
select 'CD' prod_cd, '2004-06-30' open_date, '2004-06-30' last_date,
1500.00 avail, 1500.00 pend) a
where c.fed_id = '999-99-9999'; /* corporate account data */
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Salem' limit 1) e
cross join
(select 'CHK' prod_cd, '2002-09-30' open_date, '2004-12-15' last_date,
23575.12 avail, 23575.12 pend union all
select 'BUS' prod_cd, '2002-10-01' open_date, '2004-08-28' last_date,
0 avail, 0 pend) a
where c.fed_id = '04-1111111';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Woburn' limit 1) e
cross join
(select 'BUS' prod_cd, '2004-03-22' open_date, '2004-11-14' last_date,
9345.55 avail, 9345.55 pend) a
where c.fed_id = '04-2222222';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Salem' limit 1) e
cross join
(select 'CHK' prod_cd, '2003-07-30' open_date, '2004-12-15' last_date,
38552.05 avail, 38552.05 pend) a
where c.fed_id = '04-3333333';
insert into account (account_id, product_cd, cust_id, open_date,
last_activity_date, status, open_branch_id,
open_emp_id, avail_balance, pending_balance)
select null, a.prod_cd, c.cust_id, a.open_date, a.last_date, 'ACTIVE',
e.branch_id, e.emp_id, a.avail, a.pend
from customer c cross join
(select b.branch_id, e.emp_id
from branch b inner join employee e on e.assigned_branch_id = b.branch_id
where b.city = 'Quincy' limit 1) e
cross join
(select 'SBL' prod_cd, '2004-02-22' open_date, '2004-12-17' last_date,
50000.00 avail, 50000.00 pend) a
where c.fed_id = '04-4444444'; /* put $100 in all checking/savings accounts on date account opened */
insert into transaction (txn_id, txn_date, account_id, txn_type_cd,
amount, funds_avail_date)
select null, a.open_date, a.account_id, 'CDT', 100, a.open_date
from account a
where a.product_cd IN ('CHK','SAV','CD','MM'); /* end data population */
sql语句操作记录的更多相关文章
- SQL语句操作大全
SQL语句操作大全 本文分为以下六个部分: 基础部分 提升部分 技巧部分 数据开发–经典部分 SQL Server基本函数部分 常识部分 一.基础 1.说明:创建数据库CREATE DATABAS ...
- SQL语句操作全集
SQL语句操作全集 下列语句部分是MySQL语句 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDAT ...
- Shell脚本中执行sql语句操作mysql的5种方法【转】
对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...
- Django中使用mysql数据库并使用原生sql语句操作
Django自身默认使用sqlite3这个轻量级的数据库,但是当我们开发网站时,sqlite3就没有mysql好,sqlite3适合一些手机上开发使用的数据库. 准备的软件mysql数据库,版本5.7 ...
- 在myeclipse中配置DB Driver(数据库用MySql),并在myeclipse执行sql语句操作
在myeclipse中配置DB Driver(数据库用MySql),并在myeclipse执行sql语句操作 MyEclipse6.5 , mysq驱动jar包为mysql-connector ...
- 【MySQL】使用SQL语句操作MySQL
前言: MySQL在工作中是最常用的数据库,但在使用Django和Flask的时候,都是使用ORM进行操作,除了select语句外,其他的SQL语句操作MySQL的能力没有啥提高,为了解决这个问题, ...
- Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库
下面是最原始的方法,用SQL语句操作数据库.后面的"Android中SQLite数据库操作(2)--SQLiteOpenHelper类"将介绍一种常用的android封装操作SQL ...
- Mysql中 查询慢的 Sql语句的记录查找
Mysql中 查询慢的 Sql语句的记录查找 慢查询日志 slow_query_log,是用来记录查询比较慢的sql语句,通过查询日志来查找哪条sql语句比较慢,这样可以对比较慢的sql可以进行优化. ...
- 监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)
原文:监控SQL:执行表中所有sql语句.记录每个语句运行时间(3) 通过执行一个 带参数的存储过程 exec OpreateTB('OpreateUser','IsRun') 更新表的数据 表 ...
随机推荐
- NOPI 导出excel 通用方法
public static byte[] ExportExcel<T>(Dictionary<string, string> columnsHeader, List<T& ...
- Apache 日志配置,包含过滤配置
最近排查支付宝交易成功后异步通知执行失败的原因,需要查看Apache的日志,发现之前一直没对日志进行设置,结果日志文件都1.5G多了,于是搜索了如何按天记录日志. 但公司的网站是通过阿里云的SLB分发 ...
- Oracle中字段的修改操作语法
对字段操作 操作方法 更新字段名 alter table TABLE_NAME rename column column_old to column_new; 添加字段 alter table T ...
- 【原】YUI Test自动化测试实例详解
测试在软件开发中至关重要,目前针对不同的开发语言,都有比较成熟的测试框架,如jUnit,cUnit,cppUnit,nUnit等,我们统称为xUnit,他们的都遵守统一的规则: 针对代码测试 断言 启 ...
- virtualBox 安装CentOS 全屏
在VirtualBox里安装CentOS系统,会遇到“增强工具”无法正常安装,主要的原因是出在Kernel 库找不到. 错误提示如下: 通过查看日志文件: cat /var/log/vboxadd-i ...
- php设计模式之:观察者模式
转载自php面向对象设计模式 之 观察者模式 问题 假如一个小贩, 他把产品的价格提升了, 不同的消费者会对此产生不同的反应.一般的编程模式无非是获取提升的价格,然后获取所有的消费者,再循环每个消费者 ...
- docker 创建新的镜像到私有仓库
docker:/data# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bd6db4127a9e centos &q ...
- linux md5 加密字符串和文件方法
linux md5 加密字符串和文件方法 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5全称是报文摘要算法(Message-Digest Algorithm 5),此算法对任意 ...
- HDU4551
简单. /* 简单题 */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include< ...
- Linux学习笔记2:如何快速的学习使用一个命令
Linux 分层 内核 库: .so 共享对象,windows:dll 动态链接库 应用程序 Linux的基本原则: 1.由目的单一的小程序组成:组合小程序完成复杂任务: 2.一切皆文件: 3.尽量避 ...