search
HomeDatabaseMysql Tutorialmysql中创建表、添加、修改介绍

下面我来介绍在mysql中对表的一些基本操作,包括对表的创建,删除,修改有需要学习的朋友可参考。

建表:就是声明列的过程

 代码如下 复制代码

 create table 表名(
  列名1 列2类型 列1参数,
  列名2 列2类型 列2参数,
  ……
  列n的声明 列n参数
 )engine myisam/innodb/bdb charset utf8/gbk/latin1…;

修改表的语法:MySQL ALTER语法中ALTER [IGNORE] TABLE tbl_name alter_spec [, alter_spec ...]

一张表创建完成后,有可能要增加或删除或修改列

添加列:

 代码如下 复制代码
 
alter table 表名 add 列名称 列类型 列数(加的列在表的最后)
alter table 表名 add 列名称 列类型 列参数 after 某列名(指定添加到某列后)
alter table 表名 add 列名称 列类型 列参数 first;(把新列加到最前面)

删除列:

 代码如下 复制代码

alter table 表名 drop 列名

修改列:

 代码如下 复制代码

alter table 表名 modify 列名 列类型 列参数;

修改列名及列类型:

 代码如下 复制代码

alter table 表名 change 旧列名 新列名 新类型 新参数;


例1

加索引

 代码如下 复制代码

mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]);


例子:

 代码如下 复制代码
mysql> alter table employee add index emp_name (name);

加主关键字的索引

 代码如下 复制代码

mysql> alter table 表名 add primary key (字段名);

例子:

 代码如下 复制代码
mysql> alter table employee add primary key(id);

加唯一限制条件的索引

 代码如下 复制代码

mysql> alter table 表名 add unique 索引名 (字段名);

例子:

 代码如下 复制代码
mysql> alter table employee add unique emp_name2(cardnumber);

MySQL ALTER语法运用:查看某个表的索引

 代码如下 复制代码

mysql> show index from 表名;


例子:

 代码如下 复制代码
mysql> show index from employee;


删除某个索引

 代码如下 复制代码

mysql> alter table 表名 drop index 索引名;


主关键字的索引549830479

 代码如下 复制代码

mysql> alter table tablename add primary key(id);

加唯一限制条件的索引549830479

 代码如下 复制代码

mysql> alter table tablename add unique emp_name2(cardnumber);

例子:

 代码如下 复制代码

mysql>alter table employee drop index emp_name;


 

修改表:增加字段:

 代码如下 复制代码
mysql> ALTER TABLE table_name ADD field_name field_type;


查看表:

 代码如下 复制代码
mysql> SELECT * FROM table_name;


修改原字段名称及类型:

 代码如下 复制代码
mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;


删除字段:

 代码如下 复制代码
MySQL ALTER TABLE table_name DROP field_name;
Statement
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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),