Detailed explanation of mysql data table operation examples
This article mainly shares with you detailed examples of mysql data table operation. I hope it can help you. First, let's take a look at how to create a data table.
Create table
Basic syntax form:
create table [if not exists] table name (field list [, index or constraint list]) [table option list];
- ##Field setting format:
Field name Type [Field attribute 1 Field attribute 2…..]Instructions:
1. You can choose the field name yourself;
2. The type is the data type learned earlier: int, tinyint, float, double, char(6), varchar(25), text, datetime.
3. There can be multiple field attributes (according to specific needs), directly separated by spaces; the main ones are as follows:
Meaning | |
---|---|
is only used for integer types, allowing the value of this field to automatically gain an increment value. Usually used to set the first field of a table, and usually used as the primary key | |
is used to set the The field is the primary key. At this time, the value of the field can "uniquely determine" a row of data | |
Set the field to be "unique", also Just don't repeat it. | |
is used to set that the field cannot be null (null). If it is not set, it will be nullable by default. | |
Field description text |
- The index is a hidden "data table" automatically maintained within the system. Its function is to greatly speed up data search!
- The data in this hidden data table is automatically sorted, and its search speed is based on this.
索引类型(要建立索引的字段名)
Form | Meaning | |
---|---|---|
key (field name) | It is just an index, it has no other effect and can only speed up the search | |
unique key (field name) | is an index, and you can also set the value of its field to not be repeated (uniqueness) | |
primary key (field name) | is an index, and it also has the function of distinguishing any row of data in the table ( In fact, it is also unique), it actually has a little more functions than unique index: uniqueness can be empty null, but the primary key cannot be empty | |
fulltext ( Field name) | ||
foreign key (field name) | references Other tables (corresponding Field names in other tables) |
约束类型 | 形式 | 含义 |
---|---|---|
主键约束 | primary key ( 字段名) | 使该设定字段的值可以用于“唯一确定一行数据”,其实就是“主键”的意思。 |
唯一约束 | unique key ( 字段名) | 使该设定字段的值具有“唯一性”,自然也是可区分的。 |
外键约束 | foreign key ( 字段名) references 其他表名(对应其他表中的字段名) | 使该设定字段的值,必须在其谁定的对应表中的对应字段中已经有该值了。 |
非空约束 | not null | 其实就是设定一个字段时写的那个“not null”属性。这个约束只能写在字段属性上 |
默认约束 | default XX值 | 其实就是设定一个字段时写的那个“default 默认值”属性,这个约束只能写在字段属性上。 |
检查约束 | check(某种判断语句) |
比如:
create table tab1 ( age tinyint,check (age>=0 and age <100) /*这就是检查约束*/ )#目前相关版本还不支持,就是说只分析,但会被忽略。
其实,主键约束,唯一约束,外键约束,只是“同一件事情的2个不同角度的说法”,他们同时也称为“主键索引”,“唯一索引”,“外键索引”。
表选项列表
表选项就是,创建一个表的时候,对该表的整体设定,主要有如下几个:
1、 charset = 要使用的字符编码,
2、 engine = 要使用的存储引擎(也叫表类型),
3、auto_increment = 设定当前表的自增长字段的初始值,默认是1
4、comment =‘该表的一些说明文字’
说明:
1,设定的字符编码是为了跟数据库设定的不一样。如果一样,就不需要设定了:因为其会自动使用数据库级别的设定;
2,engine(存储引擎)在代码层面,就是一个名词:InnoDB, MyIsam, BDB, archive, Memory。默认是InnoDB。
存储引擎
存储引擎是将数据存储到硬盘的“机制”。
不同的存储引擎,其实主要是从2个大的层面来设计存储机制:
尽可能快的速度;
尽可能多的功能;
选择不同的存储引擎,就是上述性能和功能的“权衡”。
大体如下:
演示:
修改表
几点说明:
修改表,是指修改表的结构——正如创建表也是设定表的结构。
创建表能做的事,修改表几乎都能做——但很不推荐去修改表,而是应该在创建表的时候就基本确定表的结构。
大体来说:
1:可以对字段进行:添加,删除,修改;
2:可以对索引进行:添加,删除表的选项,通常“都是修改”,即使不写任何表选项,他们都有其默认值。
常见几个:
操作类型 | 表达式 |
---|---|
添加字段 | alter table 表名 add [column] 新字段名 字段类型 [字段属性列表] |
修改字段(并可改名) | alter table 表名 change [column] 旧字段名 新字段名 新字段类型 [新字段属性列表] |
删除字段 | alter table 表名 drop [column] 字段名 |
添加普通索引 | alter table 表名 add key [索引名] (字段名1[,字段名2,…]) |
添加唯一索引(约束) | alter table 表名 add unique key (字段名1[,字段名2,…]) |
添加主键索引(约束) | alter table 表名 add primary key (字段名1[,字段名2,…]) |
修改表名 | alter table 旧表名 rename [to] 新表名 |
删除表 | drop table 【if exists】 表名 |
其他表的相关语句:
操作类型 | 表达式 |
---|---|
显示当前数据库中的所有表 | show tables |
显示某表的结构 | desc 表名; 或:describe 表名 |
显示某表的创建语句 | show create table 表名 |
重命名表 | rename table 旧表名 to 新表名 |
从已有表复制表结构 | create table [if not exists] 新表名 like 原表名 |
演示复制表结构:
创建表tab_int,显示表创建语句
将tab_int复制给tab_int_bak,显示tab_int_bak表创建语句,与tab_int一致
相关推荐:
The above is the detailed content of Detailed explanation of mysql data table operation examples. 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, 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.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

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.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
