当前位置:网站首页>Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables

Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables

2022-07-01 14:34:00 getupos

1. Create database

Data table belongs to database , Before creating the database , You should use use < Database name > Specify the database in which the operation is performed .

1.1 Use create Statement create data table

Examples are as follows

First create and select the database

create database company;
use company;
create table emp
(
id int,
name varchar(25),
sex tinyint,
salary float
);

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

《MySQL It's easy to get started 》 The first 4 Chapter : Creation, modification and deletion of data table _ Field name

2. Look at the data table structure

2.1 View table basic structure

Use describe/desc Statement to view table field information .

Illustrate with examples

Enter the following code

desc company;

     
  • 1.

《MySQL It's easy to get started 》 The first 4 Chapter : Creation, modification and deletion of data table _ data type _02

2.2 Look at the detailed structure of the table

show create table emp;

     
  • 1.

《MySQL It's easy to get started 》 The first 4 Chapter : Creation, modification and deletion of data table _ data type _03

3. Modify data sheet

After the data table is created , You can also modify the data table according to the actual needs

3.1 Modify data table name

Table names are uniquely defined in a database , The database system distinguishes different tables by table names

Data table update code format

alter table < The old name of the table > rename to < The new name of the table >

     
  • 1.

for example

take company In the database emp Change the table to emps

Input sql sentence

show tables;
alter table emp rename to emps;
show tables;

     
  • 1.
  • 2.
  • 3.

《MySQL It's easy to get started 》 The first 4 Chapter : Creation, modification and deletion of data table _sql_04

3.2 Modify field data type

Change the data type of the field , Is to convert the data type of a field into another data type . stay MySQL The syntax format of modifying the field data type in is as follows :

ALTER TABLE< Table name >MODIFY< Field name >< data type >;

     
  • 1.

The main parameters are introduced as follows .
Table name : Refers to the name of the table where the field of the data type to be modified is located .

Field name : Refers to the field to be modified .

data type : Refers to the new data type of the modified field .

give an example

Check the data table before operation name type

desc emps;

     
  • 1.

《MySQL It's easy to get started 》 The first 4 Chapter : Creation, modification and deletion of data table _ Field name _05

modify name

alter table emps modify name varchar(20);

     
  • 1.

《MySQL It's easy to get started 》 The first 4 Chapter : Creation, modification and deletion of data table _sql_06

3.4 Modify the field name of the data table

After the field names in the data table are set , It's not static , You can modify the field name as needed .MySQL The syntax format of modifying the table field name in is as follows :

ALTER TABLE< Table name >CHANGE< Old field name >< new field name >< New data types >;

     
  • 1.

The main parameters are introduced as follows .

Table name : The data table of the field name to be modified . Old field name : Refers to the field name before modification .
new field name : Refers to the modified field name .

New data types : Refers to the modified data type , If you don't need to modify the data type of the field , You can set the new data type to be the same as the original , But the data type cannot be empty .

give an example

原网站

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