1. 创建和管理数据库
CREATE DATABASE mytest1;
SHOW CREATE DATABASE mytest1;
CREATE DATABASE mytest2 CHARACTER SET 'gbk';
SHOW CREATE DATABASE mytest2;
CREATE DATABASE IF NOT EXISTS mytest2 CHARACTER SET 'utf8';
CREATE DATABASE IF NOT EXISTS mytest3 CHARACTER SET 'utf8';
SHOW DATABASES;
SHOW DATABASES;
USE atguigudb;
SHOW TABLES;
SELECT DATABASE() FROM DUAL;
SHOW TABLES FROM mysql;
SHOW CREATE DATABASE mytest2;
ALTER DATABASE mytest2 CHARACTER SET 'utf8';
DROP DATABASE mytest1;
SHOW DATABASES;
DROP DATABASE IF EXISTS mytest1;
DROP DATABASE IF EXISTS mytest2;
USE atguigudb;
SHOW CREATE DATABASE atguigudb;
SHOW TABLES;
CREATE TABLE IF NOT EXISTS myemp1(
id INT,
emp_name VARCHAR(15),
hire_date DATE
);
DESC myemp1;
SHOW CREATE TABLE myemp1;
SELECT * FROM myemp1;
CREATE TABLE myemp2
AS
SELECT employee_id,last_name,salary
FROM employees;
DESC myemp2;
DESC employees;
SELECT *
FROM myemp2;
CREATE TABLE myemp3
AS
SELECT e.employee_id emp_id,e.last_name lname,d.department_name
FROM employees e JOIN departments d
ON e.department_id = d.department_id;
SELECT *
FROM myemp3;
DESC myemp3;
CREATE TABLE employees_copy
AS
SELECT *
FROM employees;
SELECT * FROM employees_copy;
CREATE TABLE employees_blank
AS
SELECT *
FROM employees
WHERE 1 = 2;
SELECT * FROM employees_blank;
DESC myemp1;
ALTER TABLE myemp1
ADD salary DOUBLE(10,2);
ALTER TABLE myemp1
ADD phone_number VARCHAR(20) FIRST;
ALTER TABLE myemp1
ADD email VARCHAR(45) AFTER emp_name;
ALTER TABLE myemp1
MODIFY emp_name VARCHAR(25) ;
ALTER TABLE myemp1
MODIFY emp_name VARCHAR(35) DEFAULT 'aaa';
ALTER TABLE myemp1
CHANGE salary monthly_salary DOUBLE(10,2);
ALTER TABLE myemp1
CHANGE email my_email VARCHAR(50);
ALTER TABLE myemp1
DROP COLUMN my_email;
RENAME TABLE myemp1
TO myemp11;
DESC myemp11;
ALTER TABLE myemp2
RENAME TO myemp12;
DESC myemp12;
DROP TABLE IF EXISTS myemp2;
DROP TABLE IF EXISTS myemp12;
SELECT * FROM employees_copy;
TRUNCATE TABLE employees_copy;
SELECT * FROM employees_copy;
DESC employees_copy;
COMMIT;
SELECT *
FROM myemp3;
SET autocommit = FALSE;
DELETE FROM myemp3;
SELECT *
FROM myemp3;
ROLLBACK;
SELECT *
FROM myemp3;
COMMIT;
SELECT *
FROM myemp3;
SET autocommit = FALSE;
TRUNCATE TABLE myemp3;
SELECT *
FROM myemp3;
ROLLBACK;
SELECT *
FROM myemp3;
CREATE DATABASE mytest;
USE mytest;
CREATE TABLE book1(
book_id INT ,rr
book_name VARCHAR(255)
);
SHOW TABLES;
DROP TABLE book1,book2;
SHOW TABLES;
2. 如何创建数据表
USE atguigudb;
SHOW CREATE DATABASE atguigudb;
SHOW TABLES;
CREATE TABLE IF NOT EXISTS myemp1(
id INT,
emp_name VARCHAR(15),
hire_date DATE
);
DESC myemp1;
SHOW CREATE TABLE myemp1;
SELECT * FROM myemp1;
CREATE TABLE myemp2
AS
SELECT employee_id,last_name,salary
FROM employees;
DESC myemp2;
DESC employees;
SELECT *
FROM myemp2;
CREATE TABLE myemp3
AS
SELECT e.employee_id emp_id,e.last_name lname,d.department_name
FROM employees e JOIN departments d
ON e.department_id = d.department_id;
SELECT *
FROM myemp3;
DESC myemp3;
CREATE TABLE employees_copy
AS
SELECT *
FROM employees;
SELECT * FROM employees_copy;
CREATE TABLE employees_blank
AS
SELECT *
FROM employees
WHERE 1 = 2;
SELECT * FROM employees_blank;
DESC myemp1;
ALTER TABLE myemp1
ADD salary DOUBLE(10,2);
ALTER TABLE myemp1
ADD phone_number VARCHAR(20) FIRST;
ALTER TABLE myemp1
ADD email VARCHAR(45) AFTER emp_name;
ALTER TABLE myemp1
MODIFY emp_name VARCHAR(25) ;
ALTER TABLE myemp1
MODIFY emp_name VARCHAR(35) DEFAULT 'aaa';
ALTER TABLE myemp1
CHANGE salary monthly_salary DOUBLE(10,2);
ALTER TABLE myemp1
CHANGE email my_email VARCHAR(50);
ALTER TABLE myemp1
DROP COLUMN my_email;
RENAME TABLE myemp1
TO myemp11;
DESC myemp11;
ALTER TABLE myemp2
RENAME TO myemp12;
DESC myemp12;
DROP TABLE IF EXISTS myemp2;
DROP TABLE IF EXISTS myemp12;
SELECT * FROM employees_copy;
TRUNCATE TABLE employees_copy;
SELECT * FROM employees_copy;
DESC employees_copy;
COMMIT;
SELECT *
FROM myemp3;
SET autocommit = FALSE;
DELETE FROM myemp3;
SELECT *
FROM myemp3;
ROLLBACK;
SELECT *
FROM myemp3;
COMMIT;
SELECT *
FROM myemp3;
SET autocommit = FALSE;
TRUNCATE TABLE myemp3;
SELECT *
FROM myemp3;
ROLLBACK;
SELECT *
FROM myemp3;
CREATE DATABASE mytest;
USE mytest;
CREATE TABLE book1(
book_id INT ,rr
book_name VARCHAR(255)
);
SHOW TABLES;
DROP TABLE book1,book2;
SHOW TABLES;