Summary of MySQL's commonly used SQL statements for creating tables, adding fields, modifying fields, and adding indexes

Summary of MySQL's commonly used SQL statements for creating tables, adding fields, modifying fields, and adding indexes

This article uses examples to describe the common MySQL SQL statements for creating tables, adding fields, modifying fields, and adding indexes. Share with you for your reference, the details are as follows:

Create a table:

DROP TABLE IF EXISTS bulletin;
CREATE TABLE bulletin(
 id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, # Primary key uid INT(11) NOT NULL DEFAULT 0, # Creator id
 context VARCHAR(600) NOT NULL DEFAULT '', # Announcement details (300 words)
 begintime DEC(20) NOT NULL DEFAULT 0, # Announcement start time endtime DEC(20) NOT NULL DEFAULT 0, # Announcement end time createtime DEC(20) NOT NULL DEFAULT 0, # Creation time modifytime DEC(20) NOT NULL DEFAULT 0 # Modification time PRIMARY KEY (`Id`),
)DEFAULT CHARSET=UTF8 TYPE=INNODB;

Modify the original field name and type:

ALTER TABLE bulletin CHANGE uid username VARCHAR(50) NOT NULL DEFAULT '';

Add a new field:

alter table bulletin add citycode varchar(6) not null default 0; # city code

1. Set the encoding when creating the database

create database test character set utf8;

2. Set the encoding when creating a table

create table test(id int primary key)DEFAULT charset=utf8;

3. Modify the database encoding

alter database test character set utf8;

4. Modify the default encoding of the table

alter table test character set utf8;

5. Modify field encoding

alter table test modify col_name varchar(50) CHARACTER SET utf8;

Adding Index Methods

1. Add a PRIMARY KEY

mysql>ALTER TABLE `table_name` ADD PRIMARY KEY (`column`)

2. Add UNIQUE (unique index)

mysql>ALTER TABLE `table_name` ADD UNIQUE (
`column`
)

3. Add INDEX (normal index)

mysql>ALTER TABLE `table_name` ADD INDEX index_name (`column`)

4. Add FULLTEXT (full-text index)

mysql>ALTER TABLE `table_name` ADD FULLTEXT (
`column`
)

5. Add multi-column index

mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column1`, `column2`, `column3` )

Readers who are interested in more MySQL-related content can check out the following topics: "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills".

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Mysql table creation commonly used SQL statements personal experience sharing
  • Detailed summary of mysql sql statements to create tables
  • How to generate Hive table creation statement comment script in MySQL metadata
  • Summary of commonly used SQL statements for creating MySQL tables

<<:  js to achieve waterfall flow layout (infinite loading)

>>:  How to modify iTunes backup path under Windows

Recommend

6 solutions to IDEA's inability to connect to the MySQL database

This article mainly introduces 6 solutions to the...

Implementation steps for setting up the React+Ant Design development environment

Basics 1. Use scaffolding to create a project and...

Detailed explanation of nginx proxy_cache cache configuration

Preface: Due to my work, I am involved in the fie...

Implementation of building Kubernetes cluster with VirtualBox+Ubuntu16

Table of contents About Kubernetes Basic environm...

Mysql case analysis of transaction isolation level

Table of contents 1. Theory SERIALIZABLE REPEATAB...

Why is it slow when using limit and offset paging scenarios?

Let’s start with a question Five years ago when I...

ElementUI implements cascading selector

This article example shares the specific code of ...

About the role of meta in HTML (collected and sorted from the Internet)

W3Cschool explains it this way The <meta> el...

Summary of MySQL lock knowledge points

The concept of lock ①. Lock, in real life, is a t...

HTML+CSS to achieve drop-down menu

1. Drop-down list example The code is as follows:...

JavaScript canvas realizes the effect of nine-square grid cutting

This article shares the specific code of canvas t...

How to use docker+devpi to build local pypi source

Some time ago, I needed to use pip downloads freq...

Example of MySQL slow query

Introduction By enabling the slow query log, MySQ...