Home Database Mysql Tutorial MySQL的增删查改语句用法示例总结_MySQL

MySQL的增删查改语句用法示例总结_MySQL

Jun 01, 2016 pm 01:00 PM
mysql

1.创建列
    

alter table tablename add colname type not null default '0′;
Copy after login

例:

  alter table mmanapp_mmanmedia add appid_id integer not null default 372;
Copy after login

2.删除列

  alter table tablename drop column colname;
Copy after login

例:

  alter table mmanapp_mmanmedia drop column appid_id;
Copy after login

3.在已经存在的列上创建外键关联

ALTER TABLE yourtablename  ADD [CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, …)  REFERENCES tbl_name (index_col_name, …)  [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]  [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
Copy after login

例:

ALTER TABLE mmanapp_mmanmedia ADD CONSTRAINT fk_mdappid FOREIGN KEY(appid_id)
Copy after login

4.删除外键关联:

  ALTER TABLE yourtablename DROP FOREIGN KEY fk_symbol;
Copy after login

例:

 ALTER TABLE mmanapp_mmanmedia DROP FOREIGN KEY fk_mdappid
Copy after login

附文档两份:
一.mysql对列和表的相关操作

增加主键

alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);
Copy after login

增加一个新列

alter table infos add ex tinyint not null default '0′;
Copy after login

删除列

alter table t2 drop column c;
Copy after login

重命名列/改变列类型

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

重命名表

alter table t1 rename t2;
Copy after login

加索引

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);
Copy after login

加主关键字的索引

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

加唯一限制条件的索引

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

删除某个索引

mysql>alter table tablename drop index emp_name;
Copy after login

二.对表增/删约束关系

InnoDB允许你用ALTER TABLE往一个表中添加一个新的 外键约束:

ALTER TABLE yourtablename
  ADD [CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, …)
  REFERENCES tbl_name (index_col_name, …)
  [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
  [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
Copy after login

记住先创建需要的索引。你也可以用ALTER TABLE往一个表添加一个自引用外键约束。
InnoDB也支持使用ALTER TABLE来移除 外键:

ALTER TABLE yourtablename DROP FOREIGN KEY fk_symbol;
Copy after login

当年创建一个外键之时,如果FOREIGN KEY子句包括一个CONSTRAINT名字,你可以引用那个名字来移除 外键。另外,当外键被创建之时,fk_symbol值被InnoDB内部保证。当你想要移除一个外键之时,要找出标记,请使用SHOW CREATE TABLE语句。例子如下:

mysql> SHOW CREATE TABLE ibtest11c\G
*************************** 1. row ***************************
    Table: ibtest11c
Create Table: CREATE TABLE ibtest11c (
 A int(11) NOT NULL auto_increment,
 D int(11) NOT NULL default '0′,
 B varchar(200) NOT NULL default ”,
 C varchar(175) default NULL,
 PRIMARY KEY (A,D,B),
 KEY B (B,C),
 KEY C (C),
 CONSTRAINT 0_38775 FOREIGN KEY (A, D)
REFERENCES ibtest11a (A, D)
ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT 0_38776 FOREIGN KEY (B, C)
REFERENCES ibtest11a (B, C)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=INNODB CHARSET=latin1
1 row in set (0.01 sec)
 
mysql> ALTER TABLE ibtest11c DROP FOREIGN KEY 0_38775;
Copy after login

InnoDB解析程序允许你在FOREIGN KEY … REFERENCES …子句中用`(backticks)把表和列名名字围起来。InnoDB解析程序也考虑到lower_case_table_names系统变量的设置。
InnoDB返回一个表的外键定义作为SHOW CREATE TABLE语句输出的一部分:

SHOW CREATE TABLE tbl_name;
Copy after login

从这个版本起,mysqldump也将表的正确定义生成到转储文件中,且并不忘记 外键。
你可以如下对一个表显示外键约束:

SHOW TABLE STATUS FROM db_name LIKE ‘tbl_name';
Copy after login

外键约束被列在输出的Comment列。
当执行外键检查之时,InnoDB对它照看着的子或父记录设置共享的行级锁。InnoDB立即检查外键约束,检查不对事务提交延迟。
要使得对有外键关系的表重新载入转储文件变得更容易,mysqldump自动在转储输出中包括一个语句设置FOREIGN_KEY_CHECKS为0。这避免在转储被重新装载之时,与不得不被以特别顺序重新装载的表相关的问题。也可以手动设置这个变量:

mysql> SET FOREIGN_KEY_CHECKS = 0;
mysql> SOURCE dump_file_name;
mysql> SET FOREIGN_KEY_CHECKS = 1;
Copy after login

如果转储文件包含对外键是不正确顺序的表,这就以任何顺序导入该表。这样也加快导入操作。设置FOREIGN_KEY_CHECKS为0,对于在LOAD DATA和ALTER TABLE操作中忽略外键限制也是非常有用的。
InnoDB不允许你删除一个被FOREIGN KEY表约束 引用的表,除非你做设置SET FOREIGN_KEY_CHECKS=0。当你移除一个表的时候,在它的创建语句里定义的约束也被移除。
如果你重新创建一个被移除的表,它必须有一个遵从于也引用它的外键约束的定义。它必须有正确的列名和类型,并且如前所述,它必须对被 引用的键有索引。如果这些不被满足,MySQL返回错误号1005 并在错误信息字符串中指向errno 150。

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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

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.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

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.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

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.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

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.

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles