当前位置:网站首页>MySQL_ Create and manage tables

MySQL_ Create and manage tables

2022-06-22 12:44:00 YHSevenWater

There are several steps to learn about creating and managing tables :

• Create database

• Create table

• Describe the various data types

• Modify table

• Delete , Empty table and rename

(1) Create database

● Create a database to store teacher information
open Navicat Medium MySQL, Right click to open the command line interface , Enter the following sql sentence . The figure below shows that the creation was successful , Finally, right-click to refresh the database , Can show teacher.

CREATE database teacher;

 Insert picture description here
 Insert picture description here
● Related other commands
View all current databases , The following figure shows nine records .

show databases;

 Insert picture description here

“ Use ” A database , Make it the current database , This should be combined with the corresponding DML Statement together .

use `teacher`;
Naming rules
The database name cannot exceed 30 Characters , Variable names are limited to 29 individual
Must contain only A–Z, a–z, 0–9, _ common 63 Characters
You cannot leave spaces between the characters of an object name
Must not have the same name as other user-defined objects
You must ensure that your field has no and reserved words 、 Database systems or common methods conflict
Keep field names and types consistent , When naming fields and specifying data types for them, be sure to ensure consistency . If the data type is an integer in a table , Then don't turn into character in another table

(2) Create table

CREATE TABLE sentence

• Must have :
– CREATE TABLE jurisdiction
– Storage space

• Must specify :
– Table name
– Name , data type , Size

• grammar

CREATE TABLE demo
			(id INT(2),
			name VARCHAR(14));

 Insert picture description here

• confirm

DESCRIBE demo;

 Insert picture description here
new table :

CREATE TABLE emp (
#int type , Self increasing 
emp_id INT AUTO_INCREMENT,
# Save at most 20 Chinese and English characters 
emp_name CHAR (20),
# The total number of digits shall not exceed 15 position 
salary DOUBLE,
# The date type 
birthday DATE,
# Primary key 
PRIMARY KEY (emp_id)
) ;

 Insert picture description here
 Insert picture description here

(3) Describe the various data types

Common data types
type explain
INT Use 4 Bytes hold integer data
CHAR(size) Fixed length character data . If not specified , The default is 1 Characters , Maximum length 255
VARCHAR(size) Variable length character data , Save according to the actual length of the string , Length... Must be specified
FLOAT(M,D) Single precision , M= Integer bit + Decimal places , D= Decimal places . D<=M<=255,0<=D<=30, Default M+D<=6
DOUBLE(M,D) Double precision . D<=M<=255,0<=D<=30, Default M+D<=15
DATE Date data , Format ’YYYY-MM-DD’
BLOB Long text data in binary form , Up to 4G
TEXT Long text data , Up to 4G

(4) Modify table

ALTER TABLE sentence

Use ALTER TABLE Statement can realize :
● Add columns to existing tables
● Modify columns in existing tables
● Delete columns from existing tables
● Rename columns in an existing table

Append a new column

ALTER TABLE emp
ADD job_id VARCHAR(15);

 Insert picture description here

 Insert picture description here

Modify a column

• You can modify the data type of the column , Dimensions and defaults

ALTER TABLE emp
MODIFY (job_id VARCHAR(30));

• Changes to the default value only affect future changes to the table

ALTER TABLE emp
MODIFY (salary double(9,2) default 1000);

Delete a column

Use DROP COLUMN Clause to delete columns that are no longer needed

ALTER TABLE emp
DROP COLUMN birthday;

 Insert picture description here
 Insert picture description here

Rename a column

Use CHANGE old_column new_column dataType Clause renames the column

ALTER TABLE emp
CHANGE emp_id id int(10);

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

(5) Delete , Empty table and rename

Delete table

• Data and structures are deleted
• All running related transactions are committed
• All relevant indexes are deleted
• DROP TABLE Statement cannot be rolled back

DROP TABLE demo;

 Insert picture description here

Clear the table

• TRUNCATE TABLE sentence :
– Delete all data in the table
– Free the storage space of the table

TRUNCATE TABLE demo1;

 Insert picture description here

• TRUNCATE Statement cannot be rolled back
• have access to DELETE Statement delete data , You can roll back
• contrast :

DELETE FROM emp;
SELECT * FROM emp;
ROLLBACK;
SELECT * FROM emp;

rename

• perform RENAME Statement change table , The name of the view
• Must be the owner of the object

ALTER table emp
RENAME TO seven;

 Insert picture description here
 Insert picture description here

原网站

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