Home > Database > Mysql Tutorial > Easy-to-understand explanation of additions, deletions, modifications and queries in MySQL database

Easy-to-understand explanation of additions, deletions, modifications and queries in MySQL database

silencement
Release: 2020-01-23 22:27:18
forward
2839 people have browsed it

Easy-to-understand explanation of additions, deletions, modifications and queries in MySQL database

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

Thinkphp3.2 Add fields, 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

The above is the detailed content of Easy-to-understand explanation of additions, deletions, modifications and queries in MySQL database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:www.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