MySQL命令(一)

2018-08-29  本文已影响9人  诺之林

目录

概览

SQL Commands

DDL

CREATE

CREATE TABLE employees_2 (
    emp_no      INT             NOT NULL,
    birth_date  DATE            NOT NULL,
    first_name  VARCHAR(14)     NOT NULL,
    last_name   VARCHAR(16)     NOT NULL,
    gender      ENUM ('M','F')  NOT NULL,    
    hire_date   DATE            NOT NULL,
    PRIMARY KEY (emp_no)
);

DESC employees_2;

ALTER

ALTER TABLE employees_2 ADD a TINYINT NOT NULL;

ALTER TABLE employees_2 MODIFY a INT NOT NULL;

ALTER TABLE employees_2 DROP a;

TRUNCATE

TRUNCATE TABLE employees_2;

DROP

DROP TABLE employees_2;

DML

INSERT

INSERT INTO employees VALUES (500000,'1958-05-01','Test','Tsukuda','M','1997-11-30');

SELECT

SELECT * FROM employees ORDER BY emp_no desc LIMIT 1;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
| 500000 | 1958-05-01 | Test       | Tsukuda   | M      | 1997-11-30 |
+--------+------------+------------+-----------+--------+------------+

UPDATE

UPDATE employees SET last_name = 'Test' WHERE first_name = 'Test'

DELETE

DELETE FROM employees WHERE first_name = 'Test'

SELECT * FROM employees ORDER BY emp_no desc LIMIT 1;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
| 499999 | 1958-05-01 | Sachin     | Tsukuda   | M      | 1997-11-30 |
+--------+------------+------------+-----------+--------+------------+

DCL

GRANT

GRANT SELECT ON employees.* TO guest@localhost IDENTIFIED BY '123456';
mycli -uguest -p123456

USE employees;

SELECT * FROM employees ORDER BY emp_no desc LIMIT 1;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
| 499999 | 1958-05-01 | Sachin     | Tsukuda   | M      | 1997-11-30 |
+--------+------------+------------+-----------+--------+------------+

INSERT INTO employees VALUES (500000,'1958-05-01','Test','Tsukuda','M','1997-11-30');
(1142, "INSERT command denied to user 'guest'@'localhost' for table 'employees'")
mycli -uroot -p123456

GRANT INSERT ON employees.* TO guest@localhost IDENTIFIED BY '123456';
mycli -uguest -p123456

USE employees;

INSERT INTO employees VALUES (500000,'1958-05-01','Test','Tsukuda','M','1997-11-30');

SELECT * FROM employees ORDER BY emp_no desc LIMIT 1;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
| 500000 | 1958-05-01 | Test       | Tsukuda   | M      | 1997-11-30 |
+--------+------------+------------+-----------+--------+------------+

REVOKE

mycli -uroot -p123456

REVOKE INSERT ON employees.* FROM guest@localhost;
mycli -uguest -p123456

USE employees;

INSERT INTO employees VALUES (500000,'1958-05-01','Test','Tsukuda','M','1997-11-30');
(1142, "INSERT command denied to user 'guest'@'localhost' for table 'employees'")

TCL

COMMIT

START TRANSACTION;

UPDATE employees SET last_name = 'Test' WHERE first_name = 'Test'

SELECT * FROM employees ORDER BY emp_no desc LIMIT 1;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
| 500000 | 1958-05-01 | Test       | Test      | M      | 1997-11-30 |
+--------+------------+------------+-----------+--------+------------+

UPDATE employees SET gender = 'F' WHERE first_name = 'Test'

SELECT * FROM employees ORDER BY emp_no desc LIMIT 1;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
| 500000 | 1958-05-01 | Test       | Test      | F      | 1997-11-30 |
+--------+------------+------------+-----------+--------+------------+

COMMIT

ROLLBACK

ROLLBACK

SELECT * FROM employees ORDER BY emp_no desc LIMIT 1;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
| 500000 | 1958-05-01 | Test       | Test      | F      | 1997-11-30 |
+--------+------------+------------+-----------+--------+------------+
START TRANSACTION;

UPDATE employees SET hire_date = '2000-01-01' WHERE first_name = 'Test';

SELECT * FROM employees ORDER BY emp_no desc LIMIT 1;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
| 500000 | 1958-05-01 | Test       | Test      | F      | 2000-01-01 |
+--------+------------+------------+-----------+--------+------------+

ROLLBACK

SELECT * FROM employees ORDER BY emp_no desc LIMIT 1;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date  |
+--------+------------+------------+-----------+--------+------------+
| 500000 | 1958-05-01 | Test       | Test      | F      | 1997-11-30 |
+--------+------------+------------+-----------+--------+------------+

参考

上一篇下一篇

猜你喜欢

热点阅读