Home > Database > SQL > body text

MySQL modifies the table structure and its functions of adding, deleting and modifying fields

coldplay.xixi
Release: 2020-06-12 09:00:38
forward
3850 people have browsed it

MySQL modifies the table structure and its functions of adding, deleting and modifying fields

MySQL modify table structure add delete modify fields

Create database

CREATE DATABASE database_name
Copy after login

Create table

CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Copy after login

Delete table

DROP TABLE IF EXISTS `user`;
Copy after login


Add field:

"ALTER TABLE  `user` ADD  `id` int(11) NOT NULL DEFAULT '0'  COMMENT 'ID'"
ALTER  TABLE  `user`  ADD  `name` VARCHAR( 20  )  CHARACTER  SET utf8 COLLATE utf8_general_ci NULL  DEFAULT NULL  COMMENT  '姓名'
Copy after login

Delete field

ALTER TABLE  `user` DROP column name
Copy after login


Rename

ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
Copy after login

Modify type

alter table t1 change b b bigint not null;  
alter table infos change list list tinyint not null default '0';
Copy after login

Add index

alter table t1 rename t2;
mysql> alter table tablename change depno depno int(5) not null;  
mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]);  
mysql> alter table tablename add index emp_name (name);加主关键字的索引  
mysql> alter table tablename add primary key(id);加唯一限制条件的索引  
mysql> alter table tablename add unique emp_name2(cardnumber);删除某个索引  
mysql>alter table tablename drop index emp_name;修改表:
Copy after login

Add fields in Thinkphp3.2, such as:

M('admin')->execute("ALTER TABLE  `admin` ADD  `id` int(11) NOT NULL DEFAULT '0'  COMMENT 'ID'");
M('admin')->execute("ALTER TABLE  `admin` ADD  `name` varchar(20) DEFAULT NULL  COMMENT '姓名'");
Copy after login

Recommended tutorial: "Mysql"

The above is the detailed content of MySQL modifies the table structure and its functions of adding, deleting and modifying fields. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:liqingbo.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template