深入浅出SQL(17)-工具代码包

该系列文章系个人读书笔记及总结性内容,任何组织和个人不得转载进行商业活动!

 

工具代码包

   

本文均为代码,是实际学习过程中的编码,仅用作关键字搜索示例:

INSERT INTO easy_drinks
(drink_name,main,amount1,second,amount2,directions)
VALUES
('Blackthorn','tonic water',1.5,'pineapple juice',1,'stir with ice,strain into cocktail glass with lemon twist'),
('Blue Moon','soda',1.5,'blueberry juice',0.75,'stir with ice,strain into cocktail glass with lemon twist'),
('Oh My Gosh','peach nectar',1,'pineapple juice',1,'stir with ice,strain into shot glass'),
('Lime Fizz','Sprite',1.5,'lime juice',0.75,'stir with ice,strain into cocktail glass'),
('Kiss on the Lips','cherry juice',2,'apricot nectar',7,'serve over ice with straw'),
('Hot Gold','peach nectar',3,'orange juice',6,'pour hot orange juice in mug and add peach nectar'),
('Lone Tree','soda',1.5,'cherry juice',0.75,'stir with ice,strain into cocktail glass'),
('Greyhound','soda',1.5,'grapefruit juice',5,'serve over ice,stir well'),
('Indian Summer','apple juice',2,'hot tea',6,'add juice to mug and top off with hot tea'),
('Bull Frog','iced tea',1.5,'lemonade',5,'serve over ice with lime slice');


INSERT INTO easy_drinks
(drink_name,main,amount1,second,amount2,directions)
VALUES
('Blackthorn's JU','tonic water',2,'pineapple juice',1,'stir with ice,strain into cocktail glass with lemon twist');

INSERT INTO easy_drinks
(drink_name,main,amount1,second,amount2,directions)
VALUES
('Blackthorn\'s JU','tonic water',2,'pineapple juice',1,'stir with ice,strain into cocktail glass with lemon twist');

SELECT * FROM easy_drinks WHERE main = 'tonic water';

SELECT * FROM easy_drinks WHERE drink_name = 'Blackthorn\'s JU';

SELECT * FROM easy_drinks WHERE drink_name = 'Blackthorn''s JU';

SELECT drink_name,main,second FROM easy_drinks WHERE main = 'soda';

SELECT drink_name,main FROM easy_drinks WHERE second = 'pineapple juice';
SELECT drink_name,second FROM easy_drinks WHERE main = 'tonic water';
SELECT drink_name FROM easy_drinks WHERE main = 'tonic water' AND second = 'pineapple juice';

SELECT drink_name FROM easy_drinks WHERE main = 'soda' AND amount1 >= 1;
SELECT drink_name,main,amount1 FROM easy_drinks WHERE main = 'soda' AND amount1 >= 1;

CREATE TABLE drink_info
(
drink_name VARCHAR(50) NOT NULL,
cost DEC(4,2) NOT NULL DEFAULT 1,
carbs DEC(4,2) NOT NULL DEFAULT 0,
color VARCHAR(20),
ice CHAR(1) NOT NULL DEFAULT 'Y',
calories INT NOT NULL DEFAULT 0
);

INSERT INTO drink_info
(drink_name,cost,carbs,color,ice,calories)
VALUES
('Blackthorn',3,8.4,'yellow','Y',33),
('Blue Moon',2.5,3.2,'blue','Y',12),
('Oh My Gosh',3.5,8.6,'orange','Y',35),
('Lime Fizz',2.5 ,5.4,'green','Y',24),
('Kiss on the Lips',5.5 ,42.5,'purple','Y',171),
('Hot Gold',3.2 ,32.1,'orange','N',135),
('Lone Tree',3.6 ,4.2,'red','Y',17),
('Greyhound',4 ,14,'yellow','Y',50),
('Indian Summer',2.8 ,7.2,'brown'   ,'N',30),
('Bull Frog',2.6 ,21.5,'fan','Y',80),
('Soda and It',3.8 ,4.7,'red','N',19);

SELECT * FROM drink_info;


SELECT drink_name,cost,calories FROM drink_info WHERE cost >= 3.5 AND calories < 50;

每一种加冰,热量高于33卡路里的黄色饮料的价格;
SELECT cost FROM drink_info WHERE calories > 33 AND color = 'yellow';

每一种碳水化合物低于4克而且加冰的饮料的名称和颜色;
SELECT drink_name,color FROM drink_info WHERE carbs < 4 AND ice = 'Y';

每一种热量大于或者等于80卡路里的饮料的价格;
SELECT cost FROM drink_info WHERE calories >= 80;

只会返回Greyhound和Kiss on the Lips的查询,并附上他
们的颜色及调制时是否加冰,不要在你的查询中使用饮料名称;
SELECT drink_name,color,ice FROM drink_info WHERE cost > 3.8;

SELECT drink_name FROM drink_info WHERE drink_name >= 'L' AND drink_name < 'M';


SELECT drink_name FROM easy_drinks WHERE main = 'cherry juice';
SELECT drink_name FROM easy_drinks WHERE second = 'cherry juice';

SELECT drink_name FROM easy_drinks WHERE main = 'cherry juice' OR second = 'cherry juice';



USE grep_list;
SHOW tables;

CREATE TABLE doughnut_ratings
(
location VARCHAR(50) NOT NULL,
time VARCHAR(10) NOT NULL,
date DATE NOT NULL,
type VARCHAR(50),
rating INT NOT NULL,
comments VARCHAR(50) NOT NULL
);


INSERT INTO doughnut_ratings
(location,time,date,type,rating,comments)
VALUES
('Krispy King','8:50 am','2018-09-27','plain glazed',10,'almost perfect'),
('Starbuzz Coffee','7:35 pm','2018-05-24','cinnamon cake',5,'stale,but tasty'),
('Duncan\'s Donuts','7:03 pm','2018-04-26','jelly',7,'not enough jelly');


INSERT INTO doughnut_ratings
(location,time,date,rating,comments)
VALUES
('Duncan\'s Donuts','8:59 am','2018-08-25',6,'greasy');

SELECT type FROM doughnut_ratings WHERE location = 'Krispy King' OR rating > 5;

SELECT type FROM doughnut_ratings WHERE rating > 5 AND type = NULL;
SELECT type FROM doughnut_ratings WHERE rating > 5 AND type IS NULL;

2018-08-20:before Flower

================================================================

SHOW DATABASES;
USE drinks;
SHOW TABLES;

SELECT * FROM drink_info;
SELECT * FROM easy_drinks;

SELECT drink_name,main,second FROM easy_drinks WHERE
main = 'cherry juice' OR 
main = 'apple juice' OR
second = 'pineapple juice' OR
second = 'blueberry juice' OR
second = 'lime juice' OR
second = 'orange juice' OR 
second = 'cherry juice' OR
second = 'grapefruit juice';

INSERT INTO easy_drinks
(drink_name,main,amount1,second,amount2,directions)
VALUES
('Blackthorn twice','tonic water',2,'pineapple juice tw',1,'stir with ice,strain into cocktail glass with lemon twist');

SELECT drink_name,main,second FROM easy_drinks
WHERE main LIKE '%juice' 
OR second LIKE '%juice%';

SELECT drink_name,main FROM easy_drinks 
WHERE main LIKE '_oda';

SELECT drink_name,main FROM easy_drinks 
WHERE main LIKE '__da';

SELECT drink_name,calories FROM drink_info
WHERE calories >=30 AND calories <= 60;

SELECT drink_name,calories FROM drink_info
WHERE calories BETWEEN 30 AND 60;




Dance to your shadow when it`s good to be living lad 
I`ll lead you to the river where the light shines so blue 
Dance to your shadow  when it`s hard to be living lad
Watch the morning coming on the land you love 

Airfalarin failarin alkarin you
Airfalarin failarin alkarin you
Airfalarin failarin we are all Airfa-
-larin failarin alkarin you

Dance to your shadow when it`s hard to be living lad
I`ll lead you to the ocean where young birds spread their wings
Dan to your shadow when it`s good to be  living lad
All through the cold wind by the blue-black hills

Airfalarin failarin alkarin you
Airfalarin failarin alkarin you
Airfalarin failarin we are all Airfa-
-larin failarin alkarin you

Blackthorn  Blue Moon   Oh My Gosh 


获取表easy_drinks中Blackthorn 、Blue Moon  、Oh My Gosh三种饮料的制作说明:

SELECT drink_name,directions FROM easy_drinks
WHERE drink_name = 'Blackthorn' 
OR drink_name = 'Blue Moon'
or drink_name = 'Oh My Gosh';

SELECT drink_name,directions FROM easy_drinks
WHERE drink_name IN ('Blackthorn','Blue Moon','Oh My Gosh');

SELECT drink_name,directions FROM easy_drinks
WHERE drink_name NOT IN ('Blackthorn','Blue Moon','Oh My Gosh');

不是juice 且 主配料的量不在1-2之间 的饮料:

SELECT * FROM easy_drinks 
WHERE NOT main LIKE '%juice'
AND NOT second LIKE '%juice%'
AND NOT amount2 BETWEEN 1 AND 2;

SELECT * FROM easy_drinks 
WHERE NOT main LIKE '%juice'
AND NOT second LIKE '%juice%'
AND NOT amount2 BETWEEN 1 AND 2
AND NOT drink_name IS NULL;

================================================================


SHOW databases;
USE drinks;
SHOW tables;
SELECT * FROM drink_info;
SELECT * FROM easy_drinks;

INSERT INTO drink_info
(drink_name,cost,carbs,color,ice,calories)
VALUES
('Blue Moon',2.5,3.2,'yellow','Y',33);

INSERT INTO drink_info
VALUES
('Blue Moon',2.5,3.2,'black','Y',33),
('Blue Moon',2.5,3.2,'brown','Y',33);

DELETE FROM drink_info WHERE drink_name = 'Blue Moon' AND color = 'blue';

DELETE FROM drink_info WHERE drink_name = 'Blue Moon' AND color = 'yellow' OR color = 'black' OR color = 'brown';

INSERT INTO drink_info
VALUES
('Blue Moon',2.5,3.2,'blue','Y',33);

INSERT INTO drink_info
VALUES
('Blackthorn',3.00,8.40,'black','Y',33);

DELETE FROM drink_info WHERE drink_name = 'Blackthorn' AND color = 'yellow';

UPDATE drink_info
SET color = 'yellow'
WHERE drink_name = 'Blackthorn' AND color = 'black';


UPDATE drink_info
SET color = 'yellow'
WHERE drink_name = 'Oh My Gosh' AND color = 'orange';

UPDATE drink_info
SET cost = 3.5 + 1
WHERE cost = 3.5;


UPDATE drink_info
SET cost = cost + 1
WHERE cost = 2.5;

================================================================


SELECT * FROM biao1 WHERE location LIKE '%吉林';
SELECT * FROM biao2 WHERE state = '吉林';

SHOW CREATE TABLE my_contacts;

CREATE TABLE flower_contacts (
    contact_id INT NOT NULL AUTO_INCREMENT,
    last_name varchar(30) DEFAULT NULL,
    first_name varchar(20) DEFAULT NULL,
    emal varchar(50) DEFAULT NULL,
    gender char(1) DEFAULT NULL,
    birthdy date DEFAULT NULL,
    profession varchar(50) DEFAULT NULL,
    location varchar(20) DEFAULT NULL,
    status varchar(100) DEFAULT NULL,
    seeking varchar(100) DEFAULT NULL,
    PRIMARY KEY (contact_id)
);

DROP TABLE flower_contacts;

INSERT INTO flower_contacts 
(last_name,seeking)
VALUES
('hua1','working,girlfriend'),
('hua2','working,girlfriend'),
('hua3','working,girlfriend');

SELECT contact_id,last_name,seeking FROM flower_contacts;

INSERT INTO flower_contacts 
(contact_id,last_name,seeking)
VALUES
(5,'hua4','working,girlfriend'),
(7,'hua5','working,girlfriend');


INSERT INTO flower_contacts 
(last_name,seeking)
VALUES
('hua6','working,girlfriend'),
('hua7','working,girlfriend'),
('hua8','working,girlfriend');

INSERT INTO flower_contacts 
(contact_id,last_name,seeking)
VALUES
(10,'hua9','working,girlfriend');

INSERT INTO flower_contacts 
(contact_id,last_name,seeking)
VALUES
(NULL,'hua10','working,girlfriend');

DELETE FROM flower_contacts WHERE contact_id = 11;

INSERT INTO flower_contacts 
(contact_id,last_name,seeking)
VALUES
(NULL,'hua10','working,girlfriend');

SELECT contact_id,last_name,seeking FROM flower_contacts;


INSERT INTO flower_contacts 
(contact_id,last_name,seeking)
VALUES
(11,'hua10','working,girlfriend');

ALTER TABLE my_contacts
ADD COLUMN contact_id INT NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (contact_id);

DROP TABLE summer_drinks;
SHOW tables;

CREATE TABLE summer_drinks
(
drink_name VARCHAR(30),
cost DEC(4,2),
burdening VARCHAR(50)
);

CREATE TABLE summer_drinks
(
drink_name VARCHAR(30),
cost DEC(4,2),
burdening_first VARCHAR(20),
burdening_second VARCHAR(20) 
);

INSERT INTO summer_drinks
VALUES
('Apple Juice',15.5,'Apple,Water'),
('Orange Juice',15.5,'Orange,Water'),
('Banana Juice',15.5,'Banana,Water'),
('Strawberry Juice',15.5,'Strawberry,Water');

SELECT * FROM summer_drinks;

ALTER TABLE summer_drinks
ADD COLUMN milliliter INT AFTER cost;

ALTER TABLE summer_drinks
DROP COLUMN milliliter;


ALTER TABLE summer_drinks
RENAME TO drinks_sum;


ALTER TABLE drinks_sum
CHANGE COLUMN milliliter meter INT;

SELECT * FROM drinks_sum;

ALTER TABLE drinks_sum
ADD COLUMN drink_id INT FIRST,
ADD COLUMN drink_des VARCHAR(100);

ALTER TABLE drinks_sum
DROP COLUMN drink_id;

ALTER TABLE drinks_sum
ADD COLUMN drink_id CHAR(2) FIRST,

ALTER TABLE drinks_sum
CHANGE COLUMN drink_id id INT NOT NULL AUTO_INCREMENT,
ADD PRIMARY KEY(id);

ALTER TABLE drinks_sum
MODIFY COLUMN drink_des VARCHAR(50);

SELECT RIGHT(burdening,5) FROM drinks_sum;

SELECT SUBSTRING_INDEX(burdening,',',1) FROM drinks_sum;

SELECT SUBSTRING(burdening,0, 6) FROM drinks_sum;

SELECT SUBSTRING('flower',1, 3);
SELECT SUBSTRING('Quadratically',5,6);
        -> 'ratica'


SELECT SUBSTRING(burdening,1,LENGTH(burdening)) FROM drinks_sum;

SELECT SUBSTRING(burdening,1, LOCATE(',',burdening) - 1) FROM drinks_sum;
SELECT SUBSTRING(burdening,LOCATE(',',burdening) + 1, LENGTH(burdening) - LOCATE(',',burdening)) FROM drinks_sum;


ALTER TABLE drinks_sum
ADD COLUMN burdening_main VARCHAR(20) AFTER burdening,
ADD COLUMN burdening_sub VARCHAR(20) AFTER burdening_main;

UPDATE drinks_sum
SET burdening_main = SUBSTRING(burdening,1, LOCATE(',',burdening) - 1),burdening_sub = SUBSTRING(burdening,LOCATE(',',burdening) + 1, LENGTH(burdening) - LOCATE(',',burdening));



UPDATE drinks_sum
SET burdening_sub = SUBSTRING(burdening,LOCATE(',',burdening) + 1, LENGTH(burdening) - LOCATE(',',burdening));


ALTER TABLE drinks_sum
DROP COLUMN burdening_main,
DROP COLUMN burdening_sub;

INSERT INTO drinks_sum
VALUES
('Watermelon',16.5,500,'Watermelon,Water','Watermelon','Water','None');



INSERT INTO drinks_sum
(drink_name,cost,meter,burdening,burdening_main,burdening_sub,drink_des)
VALUES
('Watermelon',16.5,500,'Watermelon,Water','Watermelon','Water','None');


id序号    drink_name名称    cost价格    meter容量    burdening_main主配料    season适应季节    sale_date售出日期;


CREATE TABLE drinks_of_four_reason
(
id INT NOT NULL AUTO_INCREMENT,
drink_name VARCHAR(30) NOT NULL,
cost DEC(4,2) NOT NULL,
meter INT NOT NULL DEFAULT 200,
burdening_main VARCHAR(30) NOT NULL DEFAULT 'Water',
season CHAR(2) NOT NULL DEFAULT '通用',
sale_date DATE NOT NULL,
PRIMARY KEY (id)
);

INSERT INTO drinks_of_four_reason
(drink_name,cost,meter,burdening_main,season,sale_date)
VALUES
('apple juice',5.5,250,'apple','春季','2018-08-29'),
('banana juice',5.5,250,'banana','夏季','2018-08-29'),
('pear juice',5.5,250,'pear','秋季','2018-08-29'),
('peach juice',5.5,250,'peach','冬季','2018-09-01'),
('orange juice',5.5,250,'orange','春季','2018-08-29'),
('watermelon juice',5.5,250,'watermelon','夏季','2018-09-01'),
('strawberry juice',5.5,250,'strawberry','秋季','2018-08-30'),
('apricot juice',5.5,250,'apricot','冬季','2018-08-30'),
('cherry juice',5.5,250,'cherry','春季','2018-08-30'),
('plum juice',5.5,250,'plum','夏季','2018-09-01'),
('apple juice',5.5,250,'apple','春季','2018-09-02'),
('banana juice',5.5,250,'banana','夏季','2018-09-02'),
('pear juice',5.5,250,'pear','秋季','2018-09-03'),
('peach juice',5.5,250,'peach','冬季','2018-09-03'),
('orange juice',5.5,250,'orange','春季','2018-08-31'),
('watermelon juice',5.5,250,'watermelon','夏季','2018-08-31'),
('strawberry juice',5.5,250,'strawberry','秋季','2018-09-01'),
('apricot juice',5.5,250,'apricot','冬季','2018-09-01'),
('cherry juice',5.5,250,'cherry','春季','2018-09-01'),
('plum juice',5.5,250,'plum','夏季','2018-08-21'),
('Apple juice',5.5,250,'Apple','春季','2018-08-21');

SELECT * FROM drinks_of_four_reason;

ALTER TABLE drinks_of_four_reason
ADD COLUMN drink_des VARCHAR(100);

UPDATE drinks_of_four_reason SET drink_des = ‘This is the best one!’ WHERE drink_name = ‘apple juice’;


UPDATE drinks_of_four_reason
SET drink_des = 
case
WHEN drink_name = 'apple juice' THEN 'I am apple juice and I am good!'
WHEN drink_name = 'banana juice' THEN 'I am banana juice and I am good!'
WHEN drink_name = 'pear juice' THEN 'I am pear juice and I am good!'
WHEN drink_name = 'peach juice' THEN 'I am peach juice and I am good!'
WHEN drink_name = 'orange juice' THEN 'I am orange juice and I am good!'
WHEN drink_name = 'watermelon juice' THEN 'I am watermelon juice and I am good!'
WHEN drink_name = 'strawberry juice' THEN 'I am strawberry juice and I am good!'
WHEN drink_name = 'apricot juice' THEN 'I am apricot juice and I am good!'
WHEN drink_name = 'cherry juice' THEN 'I am cherry juice and I am good!'
WHEN drink_name = 'plum juice' THEN 'I am plum juice and I am good!'
ELSE ''
END;


SELECT * 
FROM drinks_of_four_reason 
ORDER BY drink_name,sale_date;




CREATE TABLE char_simple
(
cs CHAR(1) NOT NULL
);

INSERT INTO char_simple
VALUES
('0'),
('1'),
('2'),
('3'),
('A'),
('B'),
('C'),
('D'),
('a'),
('b'),
('c'),
('d'),
('+'),
('-'),
('*'),
('/');


CREATE TABLE numbers
(
n1 CHAR(1) NOT NULL,
n2 CHAR(1) NOT NULL
);

INSERT INTO numbers
VALUES
('0','5'),
('0','4'),
('1','4'),
('0','1'),
('0','3'),
('0','3'),
('1','5'),
('1','1'),
('1','3'),
('1','3');



SELECT * 
FROM drinks_of_four_reason 
WHERE season = '夏季'
ORDER BY drink_name,sale_date;

SELECT drink_name,sale_date
FROM drinks_of_four_reason 
ORDER BY sale_date;

SELECT drink_name,sale_date
FROM drinks_of_four_reason 
ORDER BY sale_date DESC;

SELECT drink_name,SUM(cost)
FROM drinks_of_four_reason
WHERE drink_name = 'banana juice';

SELECT drink_name,SUM(cost)
FROM drinks_of_four_reason
GROUP BY drink_name;


SELECT drink_name,SUM(cost)
FROM drinks_of_four_reason
GROUP BY drink_name
ORDER BY SUM(cost) DESC;

UPDATE drinks_of_four_reason
SET cost = 
CASE
WHEN drink_name = 'apple juice' THEN cost - 2
WHEN drink_name = 'banana juice' THEN cost - 1.5
WHEN drink_name = 'pear juice' THEN cost - 1
WHEN drink_name = 'peach juice' THEN cost - 0.5
WHEN drink_name = 'orange juice' THEN cost
WHEN drink_name = 'watermelon juice' THEN cost + 0.5
WHEN drink_name = 'strawberry juice' THEN cost + 1
WHEN drink_name = 'apricot juice' THEN cost + 1.5
WHEN drink_name = 'cherry juice' THEN cost + 2
WHEN drink_name = 'plum juice' THEN 3
ELSE 1
END;

SELECT drink_name,AVG(cost)
FROM drinks_of_four_reason
GROUP BY drink_name
ORDER BY AVG(cost) DESC;

SELECT drink_name,MAX(cost)
FROM drinks_of_four_reason
GROUP BY drink_name
ORDER BY MAX(cost) DESC;


SELECT drink_name,COUNT(id)
FROM drinks_of_four_reason
WHERE drink_name = 'orange juice';

SELECT DISTINCT sale_date
FROM drinks_of_four_reason
WHERE drink_name = 'orange juice'
ORDER BY sale_date DESC;

SELECT COUNT(DISTINCT sale_date)
FROM drinks_of_four_reason
WHERE drink_name = 'orange juice';

SELECT DISTINCT sale_date
FROM drinks_of_four_reason
WHERE drink_name = 'orange juice'
ORDER BY sale_date DESC
LIMIT 2;

SELECT DISTINCT sale_date
FROM drinks_of_four_reason
WHERE drink_name = 'orange juice'
ORDER BY sale_date DESC
LIMIT 1,1;


SELECT DISTINCT season
FROM drinks_of_four_reason
ORDER BY season;

CREATE DATABASE hq_contacts;

CREATE TABLE drink_list
(
id INT NOT NULL AUTO_INCREMENT,
drink_name VARCHAR(30) NOT NULL,
cost DEC(4,2) NOT NULL DEFAULT 20.00,
meter INT NOT NULL DEFAULT 200,
burdening VARCHAR(100) NOT NULL DEFAULT 'watter',
drink_des VARCHAR(100),
saler VARCHAR(20),
sale_date DATE,
PRIMARY KEY(id)
);

INSERT INTO drink_list
(drink_name,cost,meter,burdening,drink_des)
VALUES
('banana juice',25,200,'banana,water','very delicious!'),
('apple peach banana juice',30,200,'apple,peach,banana,water','very delicious!');

INSERT INTO drink_list
(drink_name,cost,meter,burdening,drink_des,saler,sale_date)
VALUES
('apple plum juice',32,200,'apple,plum,water','very delicious!','F.N','2018-09-01');

SELECT * FROM drink_list;

CREATE TABLE drink_info
(
id INT NOT NULL AUTO_INCREMENT,
drink_name VARCHAR(30) NOT NULL,
cost DEC(4,2) NOT NULL DEFAULT 20.00,
meter INT NOT NULL DEFAULT 200,
drink_des VARCHAR(100),
PRIMARY KEY(id)
);

CREATE TABLE burdenings 
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30),
drink_id INT NOT NULL,
CONSTRAINT drink_info_id_fk
FOREIGN KEY(drink_id)
REFERENCES drink_info(id)
);

ALTER TABLE drink_list
ADD COLUMN burdening_main VARCHAR(20) AFTER burdening;

UPDATE drink_list
SET burdening_main = SUBSTRING_INDEX(burdening,',',1);

ALTER TABLE drink_list
DROP COLUMN burdening_main;

2147483647
1525516953000

CREATE TABLE operation_list
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
op_type VARCHAR(100) NOT NULL,
op_des BLOB NOT NULL,
op_time BIGINT NOT NULL
);

ALTER TABLE operation_list
CHANGE op_des op_des BLOB NOT NULL;



ALTER TABLE operation_list
CHANGE op_type op_type VARCHAR(100) NOT NULL;


ALTER TABLE operation_list
CHANGE op_time op_time BIGINT NOT NULL;



INSERT INTO drink_list
(drink_name,cost,meter,burdening,drink_des)
VALUES
('banana juice',25,200,'banana,water','very delicious!'),
('apple peach banana juice',30,200,'apple,peach,banana,water','very delicious!');


================================================================

DROP TABLE drink_list;

CREATE TABLE drink_list
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
drink_name VARCHAR(30) NOT NULL,
cost DEC(4,2) NOT NULL DEFAULT 20.00,
meter INT NOT NULL DEFAULT 200,
burdening VARCHAR(50) NOT NULL DEFAULT 'watter',
burdening_main VARCHAR(20) NOT NULL DEFAULT 'watter',
drink_des VARCHAR(100),
saler VARCHAR(20),
sale_date DATE
);




INSERT INTO drink_list
(drink_name,cost,meter,burdening,burdening_main,drink_des)
VALUES
('apple peach juice',30,200,'apple,peach,water','apple','apple peach mixture also is good!'),
('apple banana juice',33,200,'apple,banana,water','apple','apple banana mixture also is good!'),
('apple plum juice',34,200,'apple,plum,water','apple','apple plum mixture also is good!');


INSERT INTO drink_list
(drink_name,cost,meter,burdening,burdening_main,drink_des,saler,sale_date)
VALUES
('apple juice',15,200,'apple,water','apple','apple juice is very delicious!','A.N','2018-09-01'),
('peach juice',20,200,'peach,water','peach','peach juice is very delicious!','P.N','2018-09-02'),
('banana juice',25,200,'banana,water','banana','banana juice is very delicious!','B.N','2018-09-03'),
('plum juice',30,200,'plum,water','plum','plum juice is very delicious!','P.N','2018-09-01'),
('apple juice',15,200,'apple,water','apple','apple juice is very delicious!','A.N','2018-09-02'),
('peach juice',20,200,'peach,water','peach','peach juice is very delicious!','P.N','2018-09-04'),
('banana juice',25,200,'banana,water','banana','banana juice is very delicious!','B.N','2018-09-05'),
('plum juice',30,200,'plum,water','plum','plum juice is very delicious!','P.N','2018-09-04'),
('apple juice',15,200,'apple,water','apple','apple juice is very delicious!','A.N','2018-09-01'),
('peach juice',20,200,'peach,water','peach','peach juice is very delicious!','P.N','2018-09-02'),
('banana juice',25,200,'banana,water','banana','banana juice is very delicious!','B.N','2018-09-03'),
('plum juice',30,200,'plum,water','plum','plum juice is very delicious!','P.N','2018-09-01'),
('apple juice',15,200,'apple,water','apple','apple juice is very delicious!','A.N','2018-09-02'),
('peach juice',20,200,'peach,water','peach','peach juice is very delicious!','P.N','2018-09-04'),
('banana juice',25,200,'banana,water','banana','banana juice is very delicious!','B.N','2018-09-05'),
('plum juice',30,200,'plum,water','plum','plum juice is very delicious!','P.N','2018-09-04');

SELECT saler FROM drink_list
WHERE NOT saler IS NULL
GROUP BY saler
ORDER BY saler;


SELECT DISTINCT saler FROM drink_list
WHERE NOT saler IS NULL;

SELECT burdening FROM drink_list
WHERE NOT burdening IS NULL
GROUP BY burdening
ORDER BY burdening;


ALTER TABLE drink_list
ADD COLUMN burdening1 VARCHAR(50) NOT NULL AFTER burdening_main,
ADD COLUMN burdening2 VARCHAR(50) NOT NULL AFTER burdening1,
ADD COLUMN burdening3 VARCHAR(50) NOT NULL AFTER burdening2;
 
SELECT * FROM drink_list;

UPDATE drink_list
SET 
burdening1 = SUBSTRING_INDEX(burdening,',',1),
burdening = SUBSTRING(burdening,LENGTH(burdening1) + 2),
burdening2 = SUBSTRING_INDEX(burdening,',',1),
burdening = SUBSTRING(burdening,LENGTH(burdening2) + 2),
burdening3 = burdening;

DROP COLUMN

ALTER TABLE drink_list
DROP COLUMN burdening1,
DROP COLUMN burdening2,
DROP COLUMN burdening3;

================================================================


CREATE TABLE drink_des
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
drink_des VARCHAR(100)
);

INSERT INTO drink_des 
(drink_des)
SELECT drink_des FROM drink_list;

SELECT * FROM drink_des;

DROP TABLE drink_des;

CREATE TABLE drink_des AS 
SELECT drink_des,meter FROM drink_list;

ALTER TABLE drink_des
ADD COLUMN id INT NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY(id);

CREATE TABLE drink_des
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
drink_des VARCHAR(100)
)AS
SELECT drink_des FROM drink_list;



CREATE TABLE drink_des
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
drink_des VARCHAR(100)
)AS
SELECT drink_des FROM drink_list;

================================================================

DROP  TABLE sale_info;

SELECT * FROM sale_info;


CREATE TABLE sale_info
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
saler VARCHAR(20),
sale_date DATE
)AS
SELECT saler, sale_date FROM drink_list 
WHERE NOT saler IS NULL
ORDER BY sale_date;

CREATE TABLE sale_info
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
si_er VARCHAR(20),
si_date DATE
)AS
SELECT saler AS si_er, sale_date AS si_date FROM drink_list 
WHERE NOT saler IS NULL
ORDER BY sale_date;

CREATE TABLE sale_info
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
si_er VARCHAR(20),
si_date DATE
)AS
SELECT saler AS si_er, sale_date AS si_date FROM drink_list
WHERE NOT saler IS NULL
ORDER BY si_date;

================================================================

SELECT * FROM drink_list;

CREATE TABLE toys
(
toy_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
toy VARCHAR(30)
);

INSERT INTO toys
(toy)
VALUES
('wan ju 1'),
('wan ju 2'),
('wan ju 3'),
('wan ju 4'),
('wan ju 5');

CREATE TABLE boys
(
boy_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
boy VARCHAR(30)
);

INSERT INTO boys
(boy)
VALUES
('boy 1'),
('boy 2'),
('boy 3'),
('boy 4'),
('boy 5');

SELECT * FROM toys;
SELECT * FROM boys;

SELECT t.toy , b.boy
FROM 
toys AS t
CROSS JOIN 
boys AS b;



SELECT *
FROM 
toys AS t
CROSS JOIN 
boys AS b;


ALTER TABLE boys
ADD COLUMN toy_id INT;

ALTER TABLE boys
MODIFY COLUMN toy_id INT NOT NULL,
ADD CONSTRAINT toys_toy_id_fk
FOREIGN KEY(toy_id)
REFERENCES toys(toy_id);

UPDATE boys
SET toy_id = 2 WHERE boy_id = 1;

UPDATE boys
SET toy_id = 4 WHERE boy_id = 2;

UPDATE boys
SET toy_id = 5 WHERE boy_id = 3;

UPDATE boys
SET toy_id = 1 WHERE boy_id = 4;

UPDATE boys
SET toy_id = 3 WHERE boy_id = 5;


ALTER TABLE boys
DROP COLUMN toy_id;

====

SELECT b.boy,t.toy 
FROM
boys AS b 
INNER JOIN
toys AS t
ON b.toy_id = t.toy_id;


SELECT b.boy,t.toy 
FROM
boys AS b 
NATURAL JOIN
toys AS t;


================================================================



SELECT boy 
FROM boys
WHERE toy_id = (
SELECT toy_id 
FROM toys 
ORDER BY toy_id 
LIMIT 1
);

SELECT boy 
FROM boys
WHERE toy_id IN (
SELECT toy_id 
FROM toys 
);


================================================================

CREATE DATABASE mark_list;
USE mark_list;

CREATE TABLE interests
(
id INT NOT NULL AUTO_INCREMENT,
interest VARCHAR(30),
PRIMARY KEY(id)
);

CREATE TABLE profession
(
id INT NOT NULL AUTO_INCREMENT,
profession VARCHAR(30),
PRIMARY KEY(id)
);

CREATE TABLE zip_code
(
zip_code INT NOT NULL,
city VARCHAR(30),
PRIMARY KEY(zip_code)
);

CREATE TABLE my_contacts
(
contact_id INT NOT NULL AUTO_INCREMENT,
last_name VARCHAR(20),
first_name VARCHAR(20),
phone VARCHAR(15),
email VARCHAR(50),
gender CHAR(1) DEFAULT 'M',
birthday DATE DEFAULT '1980-01-01',
prof_id INT NOT NULL,
CONSTRAINT profession_id_fk
FOREIGN KEY(prof_id)
REFERENCES profession(id),
zip_code INT NOT NULL,
CONSTRAINT zip_code_zip_code_fk
FOREIGN KEY(zip_code)
REFERENCES zip_code(zip_code),
PRIMARY KEY (contact_id)
);

CREATE TABLE contact_interest
(
contact_id INT NOT NULL,
CONSTRAINT my_contacts_contact_id_fk
FOREIGN KEY(contact_id)
REFERENCES my_contacts(contact_id),
interest_id INT NOT NULL,
CONSTRAINT interests_id_fk
FOREIGN KEY(interest_id)
REFERENCES interests(id)
);



INSERT INTO profession
(profession)
VALUES
('software'),
('medical');

INSERT INTO zip_code
(zip_code,city)
VALUES
(10001,'朝阳'),
(10002,'海淀');

INSERT INTO interests
(interest)
VALUES
('game'),
('travel');

INSERT INTO my_contacts
(last_name,first_name,phone,email,gender,birthday,prof_id,zip_code)
VALUES
('Joy','HQ','15612121212','joy@qq.com','M','1991-01-01',1,10001),
('Mary','DM','13212121212','mary@qq.com','F','1992-01-01',1,10002);

INSERT INTO contact_interest
(contact_id,interest_id)
VALUES
(1,1),
(2,2);


UPDATE my_contacts
SET email = 'mary@qq.com'
WHERE last_name = 'Mary';

SELECT * FROM my_contacts;
SELECT * FROM contact_interest;
SELECT * FROM profession;
SELECT * FROM zip_code;
SELECT * FROM interests;


================================================================

CREATE TABLE job_current
(
contact_id INT NOT NULL,
title VARCHAR(30),
salary INT,
start_date DATE,
CONSTRAINT job_current_my_contacts_contact_id_fk
FOREIGN KEY(contact_id)
REFERENCES my_contacts(contact_id),
PRIMARY KEY(contact_id)
);

CREATE TABLE job_desired
(
contact_id INT NOT NULL,
title VARCHAR(30),
salary_low INT,
salary_high INT,
years_exp INT,
CONSTRAINT job_desired_my_contacts_contact_id_fk
FOREIGN KEY(contact_id)
REFERENCES my_contacts(contact_id),
PRIMARY KEY(contact_id)
);

CREATE TABLE job_list
(
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(30),
salary INT,
years INT,
des VARCHAR(100),
PRIMARY KEY(id)
);

INSERT INTO job_current
(contact_id,title,salary,start_date)
VALUES
(1,'EngineerM',1000,'2014-09-01'),
(2,'EngineerS',2000,'2014-09-01');


INSERT INTO job_desired
(contact_id,title,salary_low,salary_high,years_exp)
VALUES
(1,'EngineerM',800,1800,3),
(2,'EngineerS',1500,2000,5);


INSERT INTO job_list
(title,salary,years,des)
VALUES
('EngineerM',1600,2,'better'),
('EngineerM',1000,3,'better'),
('EngineerS',1600,4,'better'),
('EngineerS',1000,5,'better');

SELECT * FROM job_current;
SELECT * FROM job_desired;
SELECT * FROM job_list;



================================================================


我们要找一名EngineerS 薪资1600 至少4年工作经验的人;

SELECT mc.last_name,mc.first_name 
FROM my_contacts mc INNER JOIN job_desired jd
ON mc.contact_id = jd.contact_id 
AND jd.title = 'EngineerS' 
AND jd.years_exp >= 4 
AND 1600 BETWEEN jd.salary_low AND jd.salary_high;

SELECT mc.last_name,mc.first_name 
FROM my_contacts mc NATURAL JOIN job_desired jd
WHERE jd.title = 'EngineerS'
AND jd.years_exp >= 4 
AND 1600 BETWEEN jd.salary_low AND jd.salary_high;


SELECT title 
FROM job_list
GROUP BY title
ORDER BY title;

SELECT mc.last_name,mc.first_name,mc.phone,jd.title
FROM my_contacts mc NATURAL JOIN job_desired jd 
WHERE jd.title IN ('EngineerM','EngineerS');

SELECT mc.last_name,mc.first_name,mc.phone,jd.title
FROM my_contacts mc NATURAL JOIN job_desired jd 
WHERE jd.title IN (
SELECT title 
FROM job_list
GROUP BY title
ORDER BY title
);

SELECT last_name,first_name
FROM my_contacts
WHERE zip_code = (
SELECT zip_code
FROM zip_code
WHERE city = '朝阳'
);

SELECT mc.last_name,mc.first_name
FROM my_contacts mc NATURAL JOIN zip_code zc
WHERE zc.city = '朝阳';

SELE

SELECT mc.last_name ln,mc.first_name fn,mc.phone phone,jd.title tt
FROM my_contacts mc NATURAL JOIN job_desired jd 
WHERE jd.title IN (
SELECT title 
FROM job_list
GROUP BY title
ORDER BY title
);

SELECT MAX(salary) 
FROM job_current;

SELECT mc.last_name, mc.first_name
FROM my_contacts mc;

SELECT mc.last_name, mc.first_name,jc.salary
FROM my_contacts mc NATURAL JOIN job_current jc
WHERE jc.salary = (
SELECT MAX(jc.salary) 
FROM job_current jc
);



SELECT mc.last_name, mc.first_name,jc.salary
FROM my_contacts mc NATURAL JOIN job_current jc
ORDER BY jc.salary DESC
LIMIT 1;

================================================================

SELECT mc.last_name, mc.first_name, 
(SELECT city
FROM zip_code
WHERE mc.zip_code = zip_code) AS city
FROM my_contacts mc;


SELECT salary
FROM my_contacts NATURAL JOIN job_current
WHERE last_name = 'Joy';

SELECT mc.last_name,mc.first_name,jc.salary
FROM my_contacts mc NATURAL JOIN job_current jc
WHERE jc.salary > (
SELECT salary
FROM my_contacts NATURAL JOIN job_current
WHERE last_name = 'Joy'
);


SELECT mc.last_name,mc.first_name,jc.salary,jc.salary - (
SELECT AVG(salary)
FROM job_current
) x_avg
FROM my_contacts mc NATURAL JOIN job_current jc;


SELECT MAX(salary)
FROM my_contacts mc NATURAL JOIN job_current jc
GROUP BY mc.zip_code
ORDER BY mc.zip_code DESC;



SELECT mc.last_name, mc.first_name
FROM my_contacts mc NATURAL JOIN job_current jc
WHERE jc.salary IN (
SELECT MAX(salary)
FROM my_contacts mc NATURAL JOIN job_current jc
GROUP BY mc.zip_code
ORDER BY mc.zip_code
);


SELECT MAX(salary)
FROM job_current;



SELECT last_name,first_name 
FROM my_contacts
WHERE zip_code IN (
SELECT mc.zip_code
FROM my_contacts mc NATURAL JOIN job_current jc
WHERE jc.salary = (
SELECT MAX(salary)
FROM job_current
)
);



SELECT *
FROM my_contacts mc NATURAL JOIN job_current jc;



SELECT mc.last_name,mc.first_name
FROM my_contacts mc 
WHERE 1 = (
SELECT COUNT(*)
FROM contact_interest
WHERE contact_id = mc.contact_id
);


SELECT mc.last_name,mc.first_name
FROM my_contacts mc
WHERE NOT EXISTS
(
SELECT * FROM job_current jc
WHERE mc.contact_id = jc.contact_id
);



SELECT mc.last_name,mc.first_name
FROM my_contacts mc
WHERE EXISTS
(
SELECT * FROM contact_interest ci
WHERE mc.contact_id = ci.contact_id
);

================================================================


SELECT mc.last_name,mc.first_name,jc.title
FROM my_contacts mc INNER JOIN job_current jc
ON mc.contact_id = jc.contact_id;

INSERT INTO my_contacts
(last_name,first_name,phone,email,gender,birthday,prof_id,zip_code)
VALUES
('July','FM','15512121212','July@qq.com','F','1993-01-01',1,10001);

SELECT mc.last_name,mc.first_name,jc.title
FROM my_contacts mc LEFT OUTER JOIN job_current jc
ON mc.contact_id = jc.contact_id;



SELECT mc.last_name,mc.first_name,pr.profession
FROM my_contacts mc LEFT OUTER JOIN profession pr
ON pr.id = mc.prof_id;


SELECT mc.last_name,mc.first_name,pr.profession
FROM profession pr LEFT OUTER JOIN my_contacts mc
ON mc.prof_id = pr.id;

UPDATE boys
SET toy_id = 4
WHERE boy_id = 4 ;
OR boy_id = 3;

SELECT b.boy,t.toy
FROM boys b LEFT OUTER JOIN toys t
ON b.toy_id = t.toy_id;

SELECT b.boy,t.toy
FROM boys b LEFT OUTER JOIN toys t
ON b.toy_id = t.toy_id;

SELECT b.boy,t.toy
FROM toys t RIGHT OUTER JOIN boys b 
ON b.toy_id = t.toy_id;

SELECT b.boy,t.toy
FROM toys t FULL OUTER JOIN boys b 
ON b.toy_id = t.toy_id;

================================================================

CREATE TABLE clown_boss
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
boss_id INT NOT NULL,
PRIMARY KEY(id)
);

INSERT INTO clown_boss
(name,boss_id)
VALUES
('B1',5),
('B2',1),
('B3',1),
('B4',3),
('B5',5);


DROP TABLE clown_boss;

ALTER TABLE clown_boss
ADD CONSTRAINT clown_boss_clown_boss_id_fk FOREIGN KEY(boss_id) REFERENCES clown_boss(id);

INSERT INTO clown_boss
(name,boss_id)
VALUES
('B6',2),
('B7',2);

SELECT cb1.name clown ,cb2.name boss 
FROM clown_boss cb1 
INNER JOIN clown_boss cb2
ON cb1.boss_id = cb2.id;

SELECT title FROM job_current
UNION ALL
SELECT title FROM job_desired
UNION ALL
SELECT title FROM job_list
ORDER BY title;


INSERT INTO job_list
(title,salary,years,des)
VALUES
('EngineerH',1600,2,'better'),
('EngineerW',1000,3,'better'),
('EngineerY',1600,4,'better'),
('EngineerA',1000,5,'better');

CREATE TABLE job_titles AS
SELECT title FROM job_current
UNION 
SELECT title FROM job_desired
UNION 
SELECT title FROM job_list
ORDER BY title;

SELECT mc.last_name,mc.phone,jc.title
FROM my_contacts mc NATURAL JOIN job_current jc
WHERE jc.title IN (
SELECT title
FROM job_list
);

SELECT mc.last_name,mc.phone, jc.title
FROM (my_contacts mc NATURAL JOIN job_current jc )
INNER JOIN job_list jl
ON jc.title = jl.title;


实践:列出薪资等于job_list表中最高薪资的职务名称;

SELECT mc.last_name, jc.title
FROM my_contacts mc NATURAL JOIN job_current jc 
WHERE jc.salary = (
SELECT MAX(salary) FROM job_list
);



实践:列出薪资高于平均薪资者的姓名;

SELECT mc.last_name
FROM my_contacts mc NATURAL JOIN job_current jc 
WHERE jc.salary > (
SELECT AVG(salary) FROM job_current
);


SELECT cb1.name clown ,cb2.name boss 
FROM clown_boss cb1 
INNER JOIN clown_boss cb2
ON cb1.boss_id = cb2.id;

SELECT cb1.name,(
SELECT name FROM clown_boss
WHERE cb1.boss_id = id
) boss
FROM clown_boss cb1;

================================================================

START TRANSACTION;
SELECT * FROM profession;
SELECT * FROM zip_code;
SELECT * FROM interests;
SELECT * FROM my_contacts;
SELECT * FROM contact_interest;
SELECT * FROM job_current;
SELECT * FROM job_desired;
SELECT * FROM job_list;
COMMIT;

================================================================

INSERT INTO profession
(profession)
VALUES
('teacher');

INSERT INTO profession
VALUES
(NULL,'student');

INSERT INTO my_contacts
(last_name,first_name,phone,email,gender,birthday,prof_id,zip_code)
VALUES
('Maik','MQ','14812121212','mail@qq.com','X','1994-01-01',3,'10002');

SELECT id FROM profession
WHERE profession = 'student'


INSERT INTO my_contacts
(last_name,first_name,phone,email,gender,birthday,prof_id,zip_code)
VALUES
('Mail','MQ','14912121212','mailmq@qq.com','X','1994-01-01',(
SELECT id FROM profession
WHERE profession = 'student'),'10002');

SELECT COUNT(*)
FROM my_contacts
WHERE gender = 'M';

ALTER TABLE my_contacts
ADD CONSTRAINT check_gender CHECK (gender IN ('M','F'));

ALTER TABLE my_contacts
DROP CONSTRAINT check_gender;

INSERT INTO my_contacts
(last_name,first_name,phone,email,gender,birthday,prof_id,zip_code)
VALUES
('Lili','LL','14012121212','lili@qq.com','X','1994-06-01',4,'10002');

查找job_desired表中EngineerS的人的信息

SELECT mc.last_name last_name,mc.first_name first_name,mc.phone,mc.email
FROM my_contacts mc NATURAL JOIN job_desired jd
WHERE jd.title = 'EngineerS';

CREATE VIEW v_EngineerS AS
SELECT mc.last_name last_name,mc.first_name first_name,mc.phone,mc.email
FROM my_contacts mc NATURAL JOIN job_desired jd
WHERE jd.title = 'EngineerS';


SELECT * FROM v_EngineerS;

SELECT * FROM (
SELECT mc.last_name last_name,mc.first_name first_name,mc.phone,mc.email
FROM my_contacts mc NATURAL JOIN job_desired jd
WHERE jd.title = 'EngineerS') AS engineerS_s;

================================================================


实践:找出联系人愿意换工作的信息,联系人信息,举例预期最低薪资的差距;

SELECT mc.last_name,mc.first_name,mc.email,jc.salary,jd.salary_low,(jc.salary - jd.salary_low) salary_raise
FROM my_contacts mc
INNER JOIN job_current jc
INNER JOIN job_desired jd
WHERE mc.contact_id = jc.contact_id AND mc.contact_id = jd.contact_id;


CREATE VIEW v_contacts AS
SELECT contact_id,last_name, first_name, gender,prof_id,zip_code
FROM my_contacts
WHERE gender != 'X'
WITH CHECK OPTION;

DROP VIEW v_contacts;

INSERT INTO v_contacts
(last_name,first_name,gender,prof_id,zip_code)
VALUES
('Lufu','LL','X',4,'10002');


INSERT INTO v_contacts
(last_name,first_name,gender,prof_id,zip_code)
VALUES
('David','DD','M',4,'10002');

===========================================================

CREATE DATABASE mark_amount;

USE mark_amount;

CREATE TABLE my_amount
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
amount DEC(9.2),
PRIMARY KEY(id)
);

INSERT INTO my_amount
(name, amount)
VALUES
('Mark',150);

SELECT * FROM my_amount;

SELECT amount 
FROM my_amount
WHERE name = 'Mark';

UPDATE my_amount 
SET amount = (amount - 100)
WHERE name = 'Mark';

UPDATE my_amount 
SET amount = 150
WHERE name = 'Mark';

SHOW CREATE TABLE my_amount;

================================================================

SELECT amount 
FROM my_amount
WHERE name = 'Mark';

START TRANSACTION;
SELECT amount 
FROM my_amount
WHERE name = 'Mark';
UPDATE my_amount 
SET amount = (amount - 100)
WHERE name = 'Mark';
SELECT amount 
FROM my_amount
WHERE name = 'Mark';
ROLLBACK;

SELECT amount 
FROM my_amount
WHERE name = 'Mark';

START TRANSACTION;
SELECT amount 
FROM my_amount
WHERE name = 'Mark';
UPDATE my_amount 
SET amount = (amount - 100)
WHERE name = 'Mark';
SELECT amount 
FROM my_amount
WHERE name = 'Mark';
COMMIT;

SELECT amount 
FROM my_amount
WHERE name = 'Mark';

================================================================

show global variables like '%log%';
show global variables like 'innodb_log%';

show binary logs;
show master logs;

show binlog events in 'binlog.000004';

================================================================

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('Hua13342278947');

CREATE USER hua1 
IDENTIFIED BY 'hua123';


SELECT Host,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Reload_priv,Shutdown_priv,Process_priv,Grant_priv,Alter_priv 
FROM User;

SELECT * FROM mysql.User WHERE User = 'hua1' \G;

利用GRANT语句把my_contacts表的SELECT操作权限授予hua1;

GRANT SELECT 
ON my_contacts
TO hua1;
    
SHOW GRANTS FOR hua1;

GRANT INSERT ON my_contacts TO hua1;
GRANT DELETE ON my_contacts TO hua1, job_list;
GRANT DELETE ON my_contacts TO hua1, job_list WITH GRANT OPTION;
GRANT SELECT(last_name,first_name) ON my_contacts TO hua1;
GRANT SELECT, INSERT ON my_contacts TO hua1;
GRANT ALL ON my_contacts TO hua1;
GRANT ALL ON my_contacts TO hua1, hua2;
GRANT SELECT ON mark_list.* TO hua1;

REVOKE SELECT
ON my_contacts
FROM hua1;


GRANT SELECT 
ON my_contacts
TO hua1
WITH GRANT OPTION;

SHOW GRANTS FOR hua1;

REVOKE GRANT OPTION  
ON my_contacts
FROM hua1;

SHOW GRANTS FOR hua1;

REVOKE SELECT
ON my_contacts
FROM hua1;

REVOKE GRANT OPTION  
FOR SELECT
ON my_contacts
FROM hua1;

REVOKE GRANT OPTION  
ON SELECT
ON my_contacts
FROM hua1;


REVOKE GRANT OPTION  
FROM SELECT
ON my_contacts
FROM hua1;


GRANT SELECT 
ON my_contacts
TO hua1
WITH GRANT OPTION;

GRANT INSERT 
ON my_contacts
TO hua1
WITH GRANT OPTION;

REVOKE INSERT
ON my_contacts
FROM hua1 CASCADE;

REVOKE INSERT
ON my_contacts
FROM hua1 RESTRICT;

REVOKE GRANT OPTION
ON my_contacts
FROM hua1;

REVOKE SELECT
ON my_contacts
FROM hua1;

GRANT SELECT(last_name,first_name) ON my_contacts TO hua1;
REVOKE SELECT(last_name,first_name) ON my_contacts FROM hua1;

CREATE ROLE hua;

GRANT SELECT 
ON my_contacts
TO hua;

SHOW GRANTS FOR hua;

GRANT hua
TO hua1;

SHOW GRANTS FOR hua1;

DROP ROLE hua;

REVOKE hua
FROM hua1;


GRANT hua
TO hua1
WITH ADMIN OPTION;

REVOKE hua
FROM hua1 CASCADE;

GRANT SELECT 
ON my_contacts
TO hua2 
IDENTIFIED BY 'hua123';
 

================================================================

CREATE TABLE stu_ranking
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
rank_num INT NOT NULL 
);

INSERT stu_ranking
(name,rank_num)
VALUES
('A',4),
('B',7),
('C',1),
('D',3),
('E',5),
('F',2),
('G',6);

SELECT name, rank_num
FROM stu_ranking
WHERE rank_num IN
(SELECT rank_num 
FROM stu_ranking
WHERE rank_num > 3 AND rank_num < 6);

SELECT name, rank_num
FROM stu_ranking
WHERE rank_num >= ALL
(SELECT rank_num 
FROM stu_ranking
WHERE rank_num > 3 AND rank_num < 6);

SELECT name, rank_num
FROM stu_ranking
WHERE rank_num > ANY
(SELECT rank_num 
FROM stu_ranking
WHERE rank_num > 3 AND rank_num < 6);

SELECT name, rank_num
FROM stu_ranking
WHERE rank_num > SOME
(SELECT rank_num 
FROM stu_ranking
WHERE rank_num > 3 AND rank_num < 6);

CREATE TEMPORARY TABLE tep_my_table AS 
SELECT name, rank_num
FROM stu_ranking
WHERE rank_num > SOME
(SELECT rank_num 
FROM stu_ranking
WHERE rank_num > 3 AND rank_num < 6);

SELECT CAST('2018-10-10' AS DATE);

SELECT CAST(2 AS DECIMAL);

SELECT CURRENT_USER;

SELECT CURRENT_DATE;
SELECT CURRENT_TIME;

ALTER TABLE stu_ranking
ADD INDEX(name);


================================================================

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值