[Cattle off the database SQL combat] 41 to 50 personal questions and answers

41. The configuration of a flip-flop AUDIT_LOG, inserted into a data table employees_test when the trigger insert relevant data to the audit

  • This is the title given pre-conditions
CREATE TABLE employees_test(
    ID INT PRIMARY KEY NOT NULL,
    NAME TEXT NOT NULL,
    AGE INT NOT NULL,
    ADDRESS CHAR(50),
    SALARY REAL
);
CREATE TABLE audit(
    EMP_no INT NOT NULL,
    NAME TEXT NOT NULL
);
-- 在SQLite环境下,建立触发器
CREATE TRIGGER audit_log AFTER INSERT ON employees_test
BEGIN
    INSERT INTO audit VALUES(NEW.ID, NEW.NAME);
END;

Running time: 19ms

Take up memory: 3304k

  • Note: The statements between BEGIN ... END is to add ';' the, MySQL because this area is still very complicated. . .

  • In MySQL environment
-- 建立触发器
CREATE TRIGGER audit_log
AFTER INSERT
ON employees_test FOR EACH ROW
INSERT INTO audit VALUES(NEW.ID, NEW.NAME);

-- 使用BEGIN ... END
-- 无法在dbeaver客户端执行 不支持delimiter语法。。。
-- 以下程序在命令行是可以执行的
DELIMITER $$
CREATE TRIGGER audit_log 
AFTER INSERT ON employees_test FOR EACH ROW
BEGIN
    INSERT INTO audit VALUES(NEW.ID, NEW.NAME);
END$$
DELIMITER ;

MySQL official document: trigger instances

42. emp_no delete duplicate records, leaving only the smallest id corresponding record.

  • This is the title given pre-conditions
CREATE TABLE IF NOT EXISTS titles_test (
    id int(11) not null primary key,
    emp_no int(11) NOT NULL,
    title varchar(50) NOT NULL,
    from_date date NOT NULL,
    to_date date DEFAULT NULL);

insert into titles_test 
values 
('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('2', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('6', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');
-- 带条件的删除操作 删除重复emp_no 保留最小id
DELETE FROM titles_test 
WHERE id NOT IN (
    SELECT id FROM (
        SELECT MIN(id) AS id FROM titles_test GROUP BY emp_no
    ) AS tt_x
);

Running time: 15ms

Take up memory: 3404k

  • Note: The following statement in MySQL is not performed
DELETE FROM titles_test 
WHERE id NOT IN (
    SELECT MIN(id) FROM titles_test GROUP BY emp_no
);
-- You can't specify target table 'titles_test' for update in FROM clause
-- 不允许在删除条件内 使用要处理的表

MySQL DELETE FROM with subquery as condition

43. All to_date as all updated 9999-01-01 is NULL, and from_date updated 2001-01-01.

  • This is the title given pre-conditions
CREATE TABLE IF NOT EXISTS titles_test (
    id int(11) not null primary key,
    emp_no int(11) NOT NULL,
    title varchar(50) NOT NULL,
    from_date date NOT NULL,
    to_date date DEFAULT NULL);

insert into titles_test 
values 
('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('2', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('6', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');
-- 更新数据 UPDATE
UPDATE titles_test 
SET to_date=NULL, from_date='2001-01-01'
WHERE to_date='9999-01-01';;

Running time: 19ms

Take up memory: 3432k

  • Note: The title here is a misunderstanding
    that is tied to whether to_date and from_date judgment
    1. When to_date = '9999-01-01', from_date is updated
    2. Or as long as from_date are updated, regardless of the value of to_date
      answer is unclear, because the data given very special, problem-solving approach is based on more than 1 case, that both bind
  • The following is a solution that is in line with the case 2 to_date conditional update, from_date full update
UPDATE titles_test
SET to_date=(
    CASE to_date
        WHEN '9999-01-01' THEN NULL
        ELSE to_date
    END
), from_date='2001-01-01';

Id = 5 and 44. The line data emp_no = 10001 replace id = 5 and emp_no = 10005, other data remain the same, using the replace implemented.

  • This is the title given pre-conditions
CREATE TABLE IF NOT EXISTS titles_test (
    id int(11) not null primary key,
    emp_no int(11) NOT NULL,
    title varchar(50) NOT NULL,
    from_date date NOT NULL,
    to_date date DEFAULT NULL);

insert into titles_test 
values 
('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('2', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('6', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');
-- 使用replace更新数据 直接全字段更新
REPLACE INTO titles_test
VALUES('5', '10005', 'Senior Engineer', '1986-06-26', '9999-01-01');

Running time: 18ms

Take up memory: 3408k

-- 使用replace函数更新数据 选择字段更新
UPDATE titles_test
SET emp_no=REPLACE(emp_no, 10001, 10005)
WHERE id=5;

Running time: 18ms

Take up memory: 3420k

  • In addition, use the UPDATE statement is also very convenient
-- 使用update更新
UPDATE titles_test SET emp_no=10001 WHERE id=5;

45. The name of the table to modify titles_test titles_2017.

  • This is the title given pre-conditions
CREATE TABLE IF NOT EXISTS titles_test (
    id int(11) not null primary key,
    emp_no int(11) NOT NULL,
    title varchar(50) NOT NULL,
    from_date date NOT NULL,
    to_date date DEFAULT NULL);

insert into titles_test 
values 
('1', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('2', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('3', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('4', '10004', 'Senior Engineer', '1995-12-03', '9999-01-01'),
('5', '10001', 'Senior Engineer', '1986-06-26', '9999-01-01'),
('6', '10002', 'Staff', '1996-08-03', '9999-01-01'),
('7', '10003', 'Senior Engineer', '1995-12-03', '9999-01-01');
-- SQLite3.7.9 修改表名的函数为RENAME TO
ALTER TABLE titles_test RENAME TO titles_2017;

Running time: 16ms

Take up memory: 3556k

  • Note: MySQL RENAME TOand RENAMEcan

46. ​​create foreign key constraints in the audit table, which corresponds to the primary key id employees_test emp_no table.

  • This is the title given pre-conditions
CREATE TABLE employees_test(
    ID INT PRIMARY KEY NOT NULL,
    NAME TEXT NOT NULL,
    AGE INT NOT NULL,
    ADDRESS CHAR(50),
    SALARY REAL
);

CREATE TABLE audit(
    EMP_no INT NOT NULL,
    create_date datetime NOT NULL
);
-- 这是看了讨论区 才知道的。。。
-- 它是先删表 再建表 在建表的同时创建外键约束
DROP TABLE audit;
CREATE TABLE audit(
    EMP_no INT NOT NULL,
    create_date datetime NOT NULL,
    FOREIGN KEY(EMP_no) REFERENCES employees_test(ID));

Running time: 17ms

Take up memory: 3424k

  • Note: OJ pit system ');' not on a separate line. . .
  • The actual environment MySQL, simply change to add
-- 添加外键约束
ALTER TABLE audit ADD FOREIGN KEY (emp_no) REFERENCES employees_test(id);

47. How to get emp_v and employees have the same data?

  • This is subject to the precondition that
    there is a view:
create view emp_v as select * from employees where emp_no >10005;
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`));
-- 最直观的 emp_v只是在employees的基础上生成的
SELECT * FROM emp_v;

Running time: 15ms

Take up memory: 3416k

-- 使用where
SELECT em.*
FROM employees AS em, emp_v AS ev 
WHERE em.emp_no=ev.emp_no;

Running time: 22ms

Take up memory: 4564k

-- 使用连接 找交集
SELECT em.*
FROM employees AS em 
INNER JOIN emp_v AS ev 
ON em.emp_no=ev.emp_no;

Running time: 23ms

Take up memory: 4588k

48. All employees get bonuses current salary increased by 10%.

  • This is the title given pre-conditions
create table emp_bonus(
    emp_no int not null,
    recevied datetime not null,
    btype smallint not null);
    CREATE TABLE `salaries` (
    `emp_no` int(11) NOT NULL,
    `salary` int(11) NOT NULL,
    `from_date` date NOT NULL,
    `to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`));
-- 借助其他表,更新薪水表数据 使得获得奖金的员工 薪水增长10%
UPDATE salaries
SET salary = salary*1.1
WHERE emp_no IN (
    SELECT emp_no FROM emp_bonus    
) AND to_date='9999-01-01';

Running time: 21ms

Take up memory: 3556k

-- 上一更新方法是有瑕疵的 不够严谨
UPDATE salaries
SET salary = salary*1.1
WHERE emp_no IN (
        -- 关键在这里 emp_bonus内的员工是有可能不存在薪水表内
    SELECT s.emp_no 
    FROM emp_bonus AS eb 
    INNER JOIN salaries AS s 
        -- 注意加上当前条件 不然emp_no会出现重复的
    ON eb.emp_no=s.emp_no AND s.to_date='9999-01-01'
);

Running time: 25ms

Take up memory: 3408k

49. generate a select count for all tables in the library (*) corresponding SQL statement

-- SQLite获得所有表的索引 通过sqlite_master表的`name`列
-- 本题需要注意小写。。。
SELECT 'select count(*) from ' || name || ';' AS cnts
FROM sqlite_master
WHERE type='table';

Running time: 19ms

Take up memory: 4808k

  • The actual environment
-- 通过information_schema.TABLES获得所有数据库的表名,指定获取哪个数据库的表名
SELECT table_name 
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA='employees';
-- 字符串拼接
SELECT CONCAT('SELECT COUNT(*) FROM ', table_name, ';') AS cnts 
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA='employees';

The MySQL database information_schema

50. The last_name all employees and first_name employees table by " '" linking

-- 简单的字符串拼接 就是需要注意'的表达
SELECT last_name || "'" || first_name FROM employees;

Running time: 25ms

Take up memory: 3404k

  • Under MySQL environment
-- 简单的字符串拼接
SELECT CONCAT(last_name, "'", first_name) FROM employees;

Complete personal practice code for

I practice SQL code has been uploaded to Github: https://github.com/slowbirdoflsh/newcode-sql-practice
for reference only ~ ~ ~

Guess you like

Origin www.cnblogs.com/slowbirdoflsh/p/11223413.html