[MYSQL Notes] Modify Table

Example table: employee information table: id(varchar(10)), name(varchar(100)), age(int)

Modify the data type of the column:

alter table 表名 modify 列名 数据类型;

Example: The number of modified characters in the column name does not exceed 100

alter table tb modify name varchar(100);

Note: Columns that store data whose beginning is not 0 and only consists of numeric characters can be modified according to the int type, varchar->int.

But if there is data in the column, in principle, the data type of the column should not be modified.

Add columns:

alter table 表名 add 列名 数据类型;

Example: Add a column birth of datetime type that can enter the employee's birth date

alter table tb add birth datetime;

Add the column to the front:

Columns created using alter table add are added to the end of the table. If you add first to the command, the new column will be added to the front.

Example: Add datetime type column birth to the front of tb

alter table tb add birth datetime first;

Add columns anywhere:

Use after to add the column to the specified position.

alter table tb add birth datetime after id;

Modify the order of columns:

The position of the column can also be modified using Modify

Example: Change the last birth to the front position

alter table tb modify borth datetome first;

 Modify the column name and modify the data type of the column:

alter table 表名 change 修改前的列名 修改后的列名 修改后的数据类型;

Example: Modify the birth of the table tb to date type, and modify the column name to birthday, and display the structure of the column after modification.

alter table tb change birth birthday date;
desc tb;

Remove columns:

alter table 表名 drop 列名;

Example: delete the column birthday of table tb

alter table tb drop birthday;

Not just columns, the drop command is also used when performing operations on databases and tables.

When a column is deleted, the data stored in the column is also deleted. This operation does not affect other columns.

Set the primary key:

Unique: After the database is created, it is necessary to find a way to determine only one eligible record from a large amount of data. For example, let each employee have a unique ID. This unique state that only determines one is called unique.

Primary key: An identifier used to identify a record among multiple records.

In order to be able to strictly determine a record, the primary key needs to have (1) no duplicate values ​​(2) no null values ​​(null) are allowed

Set the primary key when creating the table:

create table 表名(列名 数据类型 primary key ...)

 Example: Create table tb1, the column as the primary key is of type int, and the column b is of type varchar (10)

create table tb1(a int primary key,b varchar(10));

 Duplicate values ​​and null values ​​are not allowed in the columns of the primary key

Unique key: You can set a unique key that "doesn't allow duplicates but can be null"

create table tb(a int unique,b varchar(10));

Make columns with automatic sequential numbering:

For lists or the serial numbers of lists, etc., we have to enter numbers as column data every time, which is not only troublesome, but also prone to errors.

Therefore, it would be much more convenient to automatically enter consecutive serial numbers such as 1, 2, 3, and 4.

Definition of columns with automatic sequential numbering:

When defining columns, make the following settings:

(1) The data type is int: (tinyint or smallint), since it is a continuous number, the data type is naturally an integer.

(2) Plus auto_increment: used to declare continuous numbering.

(3) Set the primary key to make the column unique: the column with the automatic serial numbering function is unique and is not repeated or empty. Such a column is often used as the primary key.

create table tb (a int auto_increment primary key, b varchar(10));

Insert records using automatic sequential numbering:

Because columns with automatic serial numbering will automatically enter numbers, use as long as you enter data in other columns.

Or take input 0 or null

insert into tb (b) values('a'),values('b'),values('c');

Set initial values ​​for consecutive numbers:

Columns with automatic serial numbering can also be set to any value.

insert into tb values(100,'aa');

The value 100 will be entered in column a, and then consecutive numbers will be assigned starting from 101, that is, the values ​​will be assigned starting from the maximum value that has been entered + 1.

Duplicate values ​​cannot be entered because the primary key property is set.

Initialization of consecutive numbers:

If you delete all the records in the table and then re-enter the records, the numbers will not be assigned from 1, but from the value of the existing maximum value + 1.

If you want to delete all records, and let the number start from 1 to be entered continuously (initialization)

alter table 表名 auto_increment=1;

When there is data in the table, if the set number is larger than the existing value, you can also reset the initial value of the number through the above statement.

Set default values ​​for columns:

create table 表名(列名 数据类型 default 默认值...);

 Example: Add the ability to automatically enter 'no name entered' if no value is entered in the name column

alter table tb modify name varchar(10) default '未输入姓名';

Create an index:

Create an index named my_id on the column id of table tb:

create index my_id on tb(id);

 Show index:

show index from 表名;

Drop index:

drop index 索引名 on 表名;

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_52043808/article/details/124179211