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!

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.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

MySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.

Yes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools