Mysql method to add int fields: use the ALTER TABLE statement, pass "ALTER TABLE table name ADD new field int (length);" or "ALTER TABLE table name ADD new field int (length) AFTER existing field ;" format to add.
#A complete field includes field name, data type and constraints. The syntax format for adding fields in MySQL is as follows:
ALTER TABLE <表名> ADD <新字段名><数据类型>[约束条件];
Copy after login
The description of the syntax format is as follows:
is the name of the data table;
is the name of the field to be added;
is the data type that the field to be added can store data, which can be int, text;
[Constraints ] is optional and is used to constrain the added fields.
This syntax format adds a new field at the last position of the table (after the last column) by default.
Example
Create a new student data table in the test database. The SQL statements and running results are as follows:
mysql> USE test;
Database changed
mysql> CREATE TABLE student (
-> id INT(4),
-> name VARCHAR(20),
-> sex CHAR(1));
Query OK, 0 rows affected (0.09 sec)
Copy after login
Use DESC to view the student table The structure, SQL statements and running results are as follows:
mysql> DESC student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
Copy after login
Use the ALTER TABLE statement to add an INT type field age, the SQL statements and running results are as follows:
mysql> ALTER TABLE student ADD age INT(4);
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
Copy after login
Use DESC to view the student table Structure, check whether the age field is added successfully. The SQL statement and running results are as follows:
mysql> DESC student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(4) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| age | int(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Copy after login
As you can see from the running results, the age field has been added to the student table, and the field is at the last position of the table. The field was added successfully.
Sometimes you need to add a field in the middle, so what if you add a field in the middle?
You need to use the AFTER keyword at this time, the syntax format is as follows:
ALTER TABLE <表名> ADD <新字段名> <数据类型> [约束条件] AFTER <已经存在的字段名>;
Copy after login
The function of AFTER is to add a new field after an existing field.
Note that you can only add a new field after an existing field, but not in front of it.
Example
Use the ALTER TABLE statement to add a field named stuno with data type INT in the student table. The stuno field is located after the name field. The SQL statement and running results are as follows:
As can be seen from the running results, the stuId field has been added to the student table, and the field is located behind the name field. The field was added successfully.
The above is the detailed content of How to add int field in mysql?. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
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.
MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.
I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.
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.
Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.
The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA
When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.