当前位置:网站首页>MySQL basic database creation foundation

MySQL basic database creation foundation

2022-06-09 09:14:00 Maximize+

Create database

 Insert picture description here
 Insert picture description here

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

value type

 Insert picture description here

The date type

 Insert picture description here

String type

 Insert picture description here

Spatial data types

 Insert picture description here
 Insert picture description here

# Create table 
# The way 1:
USE atguigudb;
SHOW CREATE DATABASE atguigudb;# By default UTF8
SHOW TABLES;
CREATE TABLE IF NOT EXISTS myemp1( # Users are required to have permission to create tables 
	id INT,
	emp_name VARCHAR(15),# Use VARCHAR To define a string , Its length must be indicated when it is used 
	hire_date DATE 
);
# View table structure 
DESC myemp1;
SHOW CREATE TABLE myemp1;

SELECT *FROM  myemp1;
# The way 2:  Based on existing tables 
CREATE TABLE myemp2
AS
SELECT employee_id,last_name,salary
FROM employees;
DESC myemp2;
DESC employees;
SELECT*FROM employees



CREATE TABLE myemp3
AS
SELECT e.employee_id,e.last_name,d.department_name
FROM employees e JOIN departments d
ON e.department_id = d.department_id

SELECT * FROM myemp3;

# Create a table employees_cpoy, Realize to employees Replication of tables , Include table data 
CREATE TABLE employees_cpoy
AS
SELECT *
FROM employees;
SELECT * FROM employees_cpoy

# Create a table employees_blank, Realize to employees Table replication , Excluding table data 

CREATE TABLE employees_blank
AS
SELECT *
FROM employees;
SELECT * FROM employees
WHERE department_id > 10000;
#where 1=2;# Mountain without edges , Tiantianhan , But I dare to fight with you .

Delete 、 modify the database

 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

DESC myemp1;
## Modify table 
# Add a field  --->alter table
ALTER TABLE myemp1
ADD salary DOUBLE(10,2);#2 Decimal places   By default, it is added to the last field position in the table 

ALTER TABLE myemp1
ADD phone_number VARCHAR(20) FIRST; #

ALTER TABLE myemp1
ADD email VARCHAR(45) AFTER emp_name;# Insert ** after 

# Modify a field 
# Generally, the field type will not be modified 
ALTER TABLE myemp1
MODIFY emp_name VARCHAR(25);

ALTER TABLE myemp1
MODIFY emp_name VARCHAR(35) DEFAULT 'aaa';


# Rename field 
ALTER TABLE myemp1
CHANGE salary monthly_salary DOUBLE(10,2);

ALTER TABLE myemp1
CHANGE email my_email VARCHAR(50);
# Delete a field 
ALTER TABLE myemp1
DROP COLUMN my_email;
## rename table 
# The way 1
RENAME TABLE myemp1
TO myemp11;
SHOW TABLES;

# The way 2
ALTER TABLE myemp2
RENAME TO myemp12;

## Delete table 
DROP TABLE myemp12;
## Clear the table 
# Clear all the data in the table , But the table structure is preserved 
SELECT *FROM employees_cpoy;
TRUNCATE TABLE employees_cpoy;	
SELECT *FROM employees_cpoy;
DESC employees_cpoy;

#DCL  in  COMMIT  and  ROLLBACK
#COMMIT: Submit data , Once executed COMMIT, The data is permanently stored in the database , This means that rollback is not possible 
#ROLLBACK: Undo Data , Once executed ROLLBACK, Data rollback can be realized , Roll back to the last time COMMIT after .

# contrast  TURNCATE TABLE  and  DELECT FROM
# The same thing : All data in the table can be deleted , At the same time, the table structure is preserved 
# Difference :
# TRUNCATE: Clear table data , Once you do this , Clear all table data , At the same time, data cannot be rolled back 
# DELECT FROM : Once you do this , All table data can be cleared ( No WHERE). At the same time, the data can be rolled back 

/* DDL and DML Explanation  ①DDL Once the operation of is executed, it cannot be rolled back . Instructions SET autocommit = FLASE Yes DDL Operation failure ( Because it's executing  DDL It must be executed once after the operation COMMIT, And this COMMIT Not affected by instructions ) ②DML By default, the operation of cannot be rolled back . But if it's being carried out DML Before , Yes  SET autocommit = false. Then the operation can be rolled back , The default is true */
# demonstration  : DELETE FROM
#1
COMMIT;# Submit data 
#2
SELECT* FROM myemp3
#3
SET autocommit = FALSE;
#4
DELETE FROM myemp3
#5
SELECT* FROM myemp3
#6
ROLLBACK;
#7
SELECT* FROM myemp3


# demonstration  : TRUNCATE TABLE
#1
COMMIT;# Submit data 
#2
SELECT* FROM myemp3
#3
SET autocommit = FALSE;
#4
TRUNCATE TABLE  myemp3
#5
SELECT* FROM myemp3
#6
ROLLBACK;
#7
SELECT* FROM myemp3


############
#MySQL8.0 New characteristics :DDL Atomization of 
# Do the following 
# If it is 5.0 Will delete book1, Report errors book2 non-existent 
# If it is 8.0 Will delete bokk1, Report a mistake later book2 And roll back book1 surface 
# Atomization : Either all or none , Cannot separate , whole 
CREATE DATABASE mytest;
USE mytest;
CREATE TABLE book1(
book_id INT,
book_name VARCHAR(255)
);
DELETE TABLE book1,book2
SHOW TABLES;

Create management table exercise

practice 1

# Create and manage table exercises 
# Create database test01_office, Indicates that the character set is utf-8, And perform the following operations under this database 
CREATE DATABASE IF NOT EXISTS test01_office CHARACTER SET 'utf8';

USE test01_office;

# Create a table  dept01
/*  Field   type  id INT(7) NAME VARCHAR(25) */
CREATE TABLE dept01(
id INT(7),
`name` VARCHAR(25)
);

# Will table departments Insert the data in the new table dept02 in 
CREATE TABLE dept02
AS
SELECT * FROM atguigudb.`departments`

# establish emp01 surface 
/*  Field   type  id INT(7) first_name VARCHAR(25) last_name VARCHAR(25) dept_id INT(7) */
CREATE TABLE emp01(
id INT(7),
first_name VARCHAR(25),
last_name VARCHAR(25),
dept_id INT(7)
);
DESC emp01;
ALTER TABLE emp01
MODIFY last_name VARCHAR(50);

# according to employees establish emp2 surface 
CREATE TABLE emp02
AS 
SELECT*FROM  atguigudb.`employees`;

SHOW TABLES;
SHOW TABLES FROM test01_office;
# Delete emp01 surface 
DROP TABLE emp01;

# Will table emp02 Rename it to emp01
#alter table emp02 rename to emp01; 
RENAME TABLE emp02 TO emp01;

# In the table dept02 neutralization emp01 Add new column  test_column
ALTER TABLE emp01 ADD test_column VARCHAR(10)
ALTER TABLE dept02 ADD test_column VARCHAR(10)

# Delete the table directly emp01 Column in department_id
ALTER TABLE emp01
DROP COLUMN department_id;
DESC emp01;

practice 2

# Create database test02_market
CREATE DATABASE IF NOT EXISTS test02_market CHARACTER SET 'utf8';
# Open database 
USE test02_market;
SHOW CREATE DATABASE test02_market;
# Create data table  customers
CREATE TABLE IF NOT EXISTS customers(
	c_num INT,
	c_name VARCHAR(50),
	c_contact VARCHAR(50),
	c_city VARCHAR(50),
	c_birth DATE
);
SHOW TABLES;
DESC customers;

# take c_contact Move field to c_birth The back of the field 
ALTER TABLE customers
MODIFY c_contact VARCHAR(50) AFTER c_birth;
DESC customers;

# take c_name Change the data type to varchar(70)
ALTER TABLE customers
MODIFY c_name VARCHAR(70);

# take c_contact The field is renamed c_phone
ALTER TABLE customers
CHANGE c_contact c_phone VARCHAR(50);

# increase c_gender Fields to c_name Back , The data type is char(1)
ALTER TABLE customers
ADD c_gender CHAR(1) AFTER c_name;

# Change table name to customers_info
RENAME TABLE customers
TO customers_info;
DESC customers_info;

# Delete field c_city
ALTER TABLE customers_info
DROP COLUMN c_city;
DESC customers_info;

practice 3

# Create database test03_company
CREATE DATABASE IF NOT EXISTS test03_company CHARACTER SET 'utf8';
# Access to database 
USE test03_company;

# Create table office
CREATE TABLE IF NOT EXISTS office(
officeCode INT,
city VARCHAR(30),
address VARCHAR(50),
country VARCHAR(50),
postalCode VARCHAR(25)
);
SHOW TABLES;

# Create table employees
CREATE TABLE IF NOT EXISTS employees(
empNum INT,
lastName VARCHAR(50),
firstName VARCHAR(50),
mobile VARCHAR(25),
`code` INT,
jobTitle VARCHAR(50),
birth DATE,
note VARCHAR(255),
sex VARCHAR(5)
);

DESC employees;

# Will table employees Of mobile Modify the field to code The back of the field 
ALTER TABLE employees
# If the modified length is unreasonable, an error will be reported 
MODIFY mobile VARCHAR(25) AFTER `code`;
DESC employees;

# Will table employees Of birth The field is renamed birthday
ALTER TABLE employees
CHANGE birth birthday DATE;

# modify sex Field , The data type is char(1)
ALTER TABLE employees
MODIFY sex CHAR(1);

# Delete field note
ALTER TABLE employees
DROP COLUMN note;
DESC employees;

# Add field name favoriate_activity, The data type is varchar(100)
ALTER TABLE employees
ADD favoriate_activity VARCHAR(100);
DESC employees;

# Table name employees The name of is changed to employees_info
RENAME TABLE employees TO employees_info;
DESC employees_info;

原网站

版权声明
本文为[Maximize+]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090849444789.html