MySQL Tutorial: Basic Operations of SQL Tables
This article mainly introduces you to the basic operations and table operations of SQL. I hope it will be helpful to friends in need!
Recommended reference tutorial: "SQL Tutorial"
SQL basic operations
Basic operations: CURD, that isadd, delete, modify, query .
According to different operation objects, we can divide the basic operations of SQL into three categories: library operations, table (field) operations and data operations.
Table operation
1 Add a new table
Basic syntax:
create table [if not exists] + 表名( 字段名称 数据类型, …… 字段名称 数据类型 /* 最后后一行,不需要加逗号 */ )[表选项];
Among them, if not exists
means
If the table name does not exist, the creation code will be executed; if the table name exists, the creation code will not be executed.
Table options are used to control the presentation form of the table. There are three types, namely:
Character set setting:
charset/ character set
Specific character set used to represent the encoding format of data storage. Commonly used character sets includeGBK
andUTF8
, etc.Collation set setting:
collate
The specific collation set represents the rules of data comparison, which depends on the character set.Storage engine:
engine
Specific storage engine, the default isInnoDB
, and commonly used ones areMyISAM
.
Since any table belongs to a certain database, when creating a table, you must first specify the specific database. Here, there are two ways to specify the database, which are:
Type 1: Explicitly specify the database to which the table belongs, example
create table if not exists test.student( name varchar(10), age int, /* 整型不需要指定具体的长度 */ grade varchar(10) /* 最后后一行,不需要加逗号 */ )charset utf8;
Type 2: Implicitly specify the database to which the table belongs, example
use test; /* use + 数据库名称,表示切换到指定的数据库,这句命令其实不加分号也可以,但不建议这么做 */create table if not exists student( name varchar(10), age int, /* 整型不需要指定具体的长度 */ grade varchar(10) /* 最后后一行,不需要加逗号 */ )charset utf8;
2 Query table
View all–> Basic syntax: show tables;
View part (fuzzy query)–> Basic syntax: show tables like 'pattern';
Among them, pattern
is the matching pattern, there are two types, namely:
##%
: Matches multiple characters;
_
: Matches a single character.
_, you need to add a backslash
\_ in front of the underscore to convert meaning operation.
show tables like '%t'; means matching all tables ending with
t.
show create table table name;
\g and
\G replaces
; semicolon in the above statement, where
\g is equivalent to semicolon, and
\G is equivalent to semicolon At the same time, rotate the query table structure
90 degrees into a vertical structure.
desc/describe/show columns from table name;
3 Update table
Here, you need to pay attention: modification of the table is divided into modifying the table itself and modifying the fields in the table.Category 1: Modify the table itself
- Modify the table name, basic syntax:
rename table old table name to new table name;
- Modify table options, basic syntax:
alter table table name table option [=] value;
Category 2: Modify fields in the table, add, modify, rename and delete
- Example:
alter table student drop age;
- Note: If data already exists in the table, deleting the field will clear all data in the field. It is irreversible, so use with caution.
- Among them, the position indicates the location where this field is stored, which is divided into
first (the first position)
and
after field name (after the specified field, Default is the last position).
##Example: - alter table student change grade class varchar(10);
- first (the first position)
and
after field name (after the specified field, the default is the last position)
. Example: - alter table student modify name char(10) after id;
- first (the first location)
and
after field name (after the specified field, the default is the last location)
. Example: - alter table student add column id int first;
- alter table table name add [ column] field name data type [column attribute] [position];
- alter table table name modify field name data type [Column Properties][Position];
重命名字段,基本语法:
alter table + 表名 + change + 旧字段名 + 新字段名 + 数据类型 + [列属性][位置];
删除字段,基本语法:
alter table + 表名 + drop+ 字段名;
4 删除表
基本语法:
/** 可以一次删除多张表 */drop table + 表1, 表2 ... ;
在这里,需要注意:此删除为不可逆操作,希望大家谨慎使用。
温馨提示:符号[]
括起来的内容,表示可选项;符号+
,则表示连接的意思。
The above is the detailed content of MySQL Tutorial: Basic Operations of SQL Tables. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.
