Assume we have a user table, and the user structure is as follows:
mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(10) | YES | | NULL |
| password | varchar(32) | YES | | NULL |
| createtime | int(10) | YES | | NULL |
| createip | int(10) | YES | | NULL |
+----------+---------------+------+-----+--------- +-------+
4 rows in set (0.01 sec)
Modify table field type modify
Category | Detailed explanation |
Basic syntax | alter table table name modify field name varchar(20); |
##Examplealter table user modify username varchar(20); | |
Example descriptionChange the type of username in the user table to varchar(20) | |
Let’s execute it and see the result:
mysql> alter table user modify username varchar(20);
Query OK, 0 rows affected (0.48 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(20) | YES | | NULL |
| password | varchar(32) | YES | | NULL |
| createtime | int(10) | YES | | NULL |
| createip | int(10) | YES | | NULL |
+----------+---------------+------+-----+--------- +-------+
4 rows in set (0.01 sec)
Add table fields
CategoryDetailed explanation | |
##Basic syntaxalter table table name add column field name type; | Example |
alter table user add column age int(3); | Example description |
Add a field as age, type is integer, length is 3 | mysql> alter table emp add column age int(3);
Query OK, 0 rows affected (0.40 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(20) | YES | | NULL |
| password | varchar(32) | YES | | NULL |
| createtime | int(10) | YES | | NULL |
| createip | int(10) | YES | | NULL |
| age | int(3) | YES | | NULL |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)
Control the order of fields when adding fieldsWe just learned to add fields. If you carefully experiment and find that every time it is added at the end, how to add it at the first one or after the pointing field? Category | Detailed explanation | Basic syntax | ALTER TABLE table name ADD field name field type AFTER field name; | Example | ALTER TABLE user ADD email VARCHAR(60) AFTER createip; | Example description | In the user table, add a field as email after createip, the type is varchar, the length is 60 | Category | Detailed explanation | ##Basic syntaxALTER TABLE table name ADD Field name Field type; | | ExampleALTER TABLE user ADD id INT(10) FIRST; | | Example descriptionIn the user table, add a field as id at the beginning, type is int, length is 10 | | ALTER TABLE user ADD email VARCHAR(60) AFTER createip;
Query OK, 0 rows affected (0.40 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| username | varchar(20) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
| createtime | int(10) | YES | | NULL | |
| createip | int(10) | YES | | NULL | |
| email | varchar(60) | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+----------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
删除表字段类别 | 详细解示 |
---|
基本语法 | alter table 表名 drop column 字段名; | 示例 | alter table user drop column age; | 示例说明 | 在user表中删除字段age |
mysql> alter table user drop column age;
Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(20) | YES | | NULL |
| password | varchar(32) | YES | | NULL |
| createtime | int(10) | YES | | NULL |
| createip | int(10) | YES | | NULL |
| email | varchar(60) | YES | | NULL |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)
Table field renameCategory | Detailed explanation | Basic syntax | alter table table name change field original name field new name field type; | Example | alter table user change email em varchar(60); | Example description | In the user table, name the email field in the field em | Detailed example: mysql> alter table user change email em varchar(60); Query OK, 0 rows affected (0.38 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| username | varchar(20) | YES | | NULL |
| password | varchar(32) | YES | | NULL |
| createtime | int(10) | YES | | NULL |
| createip | int(10) | YES | | NULL |
| em | varchar(60) | YES | | NULL |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)
Modify the order of table fieldsIn the previous field addition and modification statements (add/change/modify), you can add one at the end Optional first|after. We have already learned how to adjust the order when adding table fields. Let's now take a look at how another change or modify can adjust the order. Let’s do a small experiment with first. Use modify to adjust the ordermysql> alter table user modify em varchar(60) first;
Query OK, 0 rows affected (0.41 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| em | varchar(60) | YES | | NULL |
| username | varchar(20) | YES | | NULL |
| password | varchar(32) | YES | | NULL |
| createtime | int(10) | YES | | NULL |
| createip | int(10) | YES | | NULL |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)
Modify table nameCategory | Detailed explanation |
---|
Basic syntax | alter table old table name rename new table name; | Example | alter table user rename new_user; | Example description | Change the user table name to new_user |
##mysql> alter table user rename new_user;
Query OK, 0 rows affected (0.35 sec)
mysql> desc new_user;
+----------+---------------+------+-----+--------- +-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+--------- +-------+
| em | varchar(60) | YES | | NULL |
| username | varchar(20) | YES | | NULL |
| password | varchar(32) | YES | | NULL |
| createtime | int(10) | YES | | NULL |
| createip | int(10) | YES | | NULL |
+----------+---------------+------+-----+--------- +-------+
5 rows in set (0.00 sec)
Next Section<?php
echo "Hello Mysql";
?> - Chapter1Why choose this course to learn PHP
- Why learn PHP?
- What is PHP
- You can learn even with z...
- Why can't some people lea...
- Chapter2PHP environment installation
- What is the development e...
- Windows environment insta...
- Linux environment install...
- Other development environ...
- Tool selection for writin...
- Chapter3php basic syntax
- PHP basic syntax
- Our first piece of PHP co...
- Variables in php - you wi...
- echo display command
- Learning php annotations
- Data types are not myster...
- PHP integer type is an in...
- PHP data type Boolean (ac...
- PHP data type string
- PHP data type floating po...
- PHP flow control if else ...
- PHP data type NULL type
- php data type array
- Resource type of php data...
- PHP data type viewing and...
- Automatic conversion and ...
- Object (will learn later)
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- Variable references for P...
- PHP basic syntax arithmet...
- PHP basic syntax assignme...
- PHP basic syntax: self-in...
- PHP basic syntax comparis...
- Logical operations of php...
- PHP basic syntax bit oper...
- PHP basic syntax: ternary...
- Chapter4PHP process control
- Process control in PHP
- PHP process control if co...
- PHP flow control if state...
- Nested if...else...elseif...
- Multiple nesting of if st...
- Use of branch structure s...
- Use of loop statements in...
- while loop
- The difference between do...
- PHP flow control for loop...
- PHP flow control goto syn...
- Chapter5Basic function syntax of PHP
- Basic function syntax of ...
- PHP function basic syntax...
- PHP custom function callb...
- PHP custom function varia...
- PHP custom function anony...
- Internal function of php ...
- Variable scope of php cus...
- Reference to parameters o...
- PHP custom function recur...
- Static variables of php c...
- php uses system built-in ...
- php file contains functio...
- PHP math commonly used fu...
- PHP function to obtain pe...
- php date validation funct...
- PHP gets localized timest...
- PHP program execution tim...
- PHP string common functio...
- Chapter6PHP arrays and data structures
- PHP arrays and data struc...
- php array definition
- PHP array calculation
- php for loop traverses in...
- php foreach traverses as...
- PHP list, each function t...
- PHP commonly used array m...
- Common functions for php ...
- Chapter7Regular expressions in PHP
- Regular expressions in PH...
- Delimiter expressed by ph...
- Atoms in php regular expr...
- Metacharacters in php reg...
- Pattern modifiers in php ...
- Tips and commonly used re...
- PHP uses regular expressi...
- Chapter8php file system
- File system
- php read file
- php creates and modifies ...
- php creates temporary fil...
- php move, copy and delete...
- php detect file attribute...
- Common functions and cons...
- php file locking mechanis...
- php directory processing ...
- php file permission setti...
- php file path function
- PHP implements file guest...
- PHP implementation exampl...
- Chapter9PHP file upload
- PHP file upload
- When uploading files, you...
- Steps to upload php files
- Precautions for php file ...
- php completes file upload...
- php multiple file upload
- PHP file upload progress ...
- Chapter10PHP image processing
- PHP image processing
- PHP image processing gd2 ...
- PHP uses image processing...
- PHP development verificat...
- php image scaling and cro...
- PHP image watermark proce...
- Chapter11PHP error handling
- Error handling
- PHP error handling prohib...
- PHP error handling error ...
- PHP error handling error ...
- PHP error handling custom...
- Chapter12Getting started with MySQL
- Getting Started with MySQ...
- Mysql database introducti...
- Mysql entertainment expla...
- mysql database installati...
- Data statement operation ...
- Mysql connect to database
- Mysql database operation
- Mysql data table operatio...
- Mysql data field operatio...
- Mysql data type
- Mysql character set
- Mysql table engine
- Mysql index
- Mysql add, delete, modify...
- Mysql add, delete, modify...
- Mysql multi-table joint q...
- Mysql addition, deletion,...
- Mysql add, delete, modify...
- DCL statement
- Learn commonly used Engli...
- Chapter13PHP operates mysql database
- PHP operates mysql databa...
- PHP database connection s...
- PHP operates the database...
- PHP database operation: m...
- PHP database operation: p...
- PHP database operation: b...
- PHP database operation to...
- The ultimate solution to ...
- Chapter14php session management and control
- session overview
- Overview of Cookies for P...
- php session control Cooki...
- PHP session control using...
- php SESSION application e...
- Session management and co...
- Chapter15Making a thief program through cURL
- php curl usage methods an...
- php curl custom get metho...
- php curl uses post to sen...
- Making a thief program th...
- Chapter16Learn commonly used English words in PHP
- List of commonly used Eng...
|