Table of Contents
1、默认约束
2、设置自增列
3、查看表定义
4、修改表名
5、修改字段的数据类型
6、修改字段名
7、添加字段
8、删除字段
9、删除外键约束
10、删除表
补充:
Home Database Mysql Tutorial MySQL优化之简单语法_MySQL

MySQL优化之简单语法_MySQL

Jun 01, 2016 pm 01:00 PM
grammar


Copy after login

1、默认约束

    --mysql
    CREATE TABLE emp
    (
    id INT DEFAULT 12
    )
Copy after login

2、设置自增列

MYSQL的自增列一定要是有索引的列,设置种子值要在表的后面设置
  --mysql
    -- 设置自增ID从N开始
    CREATE TABLE emp (
    ID INT  PRIMARY KEY AUTO_INCREMENT
    ) AUTO_INCREMENT = 100 ; --(设置自增ID从100开始)
Copy after login
设置自增列的步长,可以分为全局级别和会话级别,如果是会话级别,那么当用户新建一个会话的时候,那么步长又回到了全局级别mysql不能设置为表级别的步长!!
mysql服务器维护着2种mysql的系统参数(系统变量):全局变量(global variables)和会话变量(session variables)。
它们的含义与区别如其各占的名称所示,session variables是在session级别的,对其的变更只会影响到本session;global variables是系统级别的,
对其的变更会影响所有新session(变更时已经存在session不受影响)至下次mysql server重启动。
注意它的变更影响不能跨重启,要想再mysql server重启时也使用新的值,那么就只有通过在命令行指定变量选项或者更改选项文件来指定,
而通过SET变更是达不到跨重启的。
每一个系统变量都有一个默认值,这个默认值是在编译mysql系统的时候确定的。
对系统变量的指定,一般可以在server启动的时候在命令行指定选项或者通过选项文件来指定
当然,大部分的系统变量,可以在系统的运行时,通过set命令指定其值。
查看系统当前默认的自增列种子值和步长值
SHOW GLOBAL VARIABLES LIKE 'auto_incre%'; -- 全局变量
Copy after login
问:如果有一张表,里面有个字段为id的自增主键,当已经向表里面插入了10条数据之后,删除了id为8,9,10的数据,再把mysql重启,
之后再插入一条数据,那么这条数据的id值应该是多少,是8,还是11?
答:如果表的类型为MyISAM,那么是11。如果表的类型为InnoDB,则id为8。
这是因为两种类型的存储引擎所存储的最大ID记录的方式不同,MyISAM表将最大的ID记录到了数据文件里,重启mysql自增主键的最大ID值也不会丢失;
而InnoDB则是把最大的ID值记录到了内存中,所以重启mysql或者对表进行了OPTIMIZE操作后,最大ID值将会丢失。
顺便说一下MYSQL获取当前表的自增值的四种方法
(1) SELECT MAX(id) FROM person   针对特定表
(2) SELECT LAST_INSERT_ID()  函数   针对任何表
(3) SELECT @@identity    针对任何表
Copy after login
@@identity 是表示的是最近一次向具有identity属性(即自增列)的表插入数据时对应的自增列的值,是系统定义的全局变量。
一般系统定义的全局变量都是以@@开头,用户自定义变量以@开头。
使用@@identity的前提是在进行insert操作后,执行select @@identity的时候连接没有关闭,否则得到的将是NULL值。
(4)  SHOW TABLE STATUS LIKE 'person' 
Copy after login
如果针对特定表,建议使用这一种方法
得出的结果里边对应表名记录中有个Auto_increment字段,里边有下一个自增ID的数值就是当前该表的最大自增ID.

3、查看表定义

 DESC emp
Copy after login

4、修改表名

ALTER TABLE emp RENAME emp2
Copy after login

5、修改字段的数据类型

将id字段的int类型改为bigint
ALTER TABLE emp2 MODIFY id BIGINT
Copy after login

6、修改字段名

MYSQL里修改字段名的时候需要加上字段的数据类型否则会报错,而CHANGE也可以只修改数据类型,实现和MODIFY同样的效果,方法是将SQL语句中的“新字段名”和“旧字段名”设置为相同的名称,只改变“数据类型”,改变数据类型,例如刚才那个例子,将id列改为bigint数据类型
ALTER TABLE emp2 CHANGE id id BIGINT
Copy after login

7、添加字段

ALTER TABLE emp2 ADD NAME NVARCHAR(200)  NULL
Copy after login

8、删除字段

MYSQL删除字段不需要添加COLUMN关键字的
 ALTER TABLE emp2 DROP NAME
Copy after login

9、删除外键约束

如果是外键约束,需要使用 DROP FOREIGN KEY,如果是主键约束需要使用DROP PRIMARY KEY
--删除外键约束
 ALTER TABLE emp2 DROP FOREIGN KEY fk_emp_dept
Copy after login
--删除主键约束
 ALTER TABLE emp2 DROP PRIMARY KEY pk_emp_dept
Copy after login

10、删除表

DROP TABLE emp2
Copy after login
但是如果要同时删除多个表或者删除之前要先判断一下
DROP TABLE IF EXISTS emp1 ,emp2
Copy after login

补充:

USE test;
-- myisam引擎
CREATE TABLE TEST(
ID int unsigned not null auto_increment,
name varchar(10) not null,
  key(name,id))engine=MYISAM auto_increment=100
;

-- innodb引擎
CREATE TABLE TESTIdentity(
ID int unsigned   not null auto_increment,
NID INT UNSIGNED ,
name varchar(10) not null,
  key(id))engine=INNODB auto_increment=100
;

--或者主键
CREATE TABLE TESTIdentity(
ID int unsigned   not null auto_increment,
NID INT UNSIGNED ,
name varchar(10) not null,
  key(id))engine=INNODB auto_increment=100
;

[Database4]
ErrorCode: -2147467259, Number: 1075
ErrorMessage: Incorrect table definition; there can be only one auto column and it must be defined as a key
alter table TESTIdentity modify column nid int auto_increment;
Copy after login
无论innodb引擎还是MYISAM引擎的表中,只能有一个自增列,并且自增列一定是索引列,无论是二级索引还是主键索引

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)

How to quickly turn your Python code into an API How to quickly turn your Python code into an API Apr 14, 2023 pm 06:28 PM

When it comes to API development, you may think of DjangoRESTFramework, Flask, and FastAPI. Yes, they can be used to write APIs. However, the framework shared today allows you to convert existing functions into APIs faster. It is Sanic . Introduction to Sanic Sanic[1] is a Python3.7+ web server and web framework designed to improve performance. It allows the use of the async/await syntax added in Python 3.5, which can effectively avoid blocking and improve response speed. Sanic is committed to providing a simple and fast way to create and launch

New type alias syntax in PHP8.0 New type alias syntax in PHP8.0 May 14, 2023 pm 02:21 PM

With the release of PHP 8.0, a new type alias syntax has been added, making it easier to use custom types. In this article, we'll take a closer look at this new syntax and its impact on developers. What is a type alias? In PHP, a type alias is essentially a variable that references the name of another type. This variable can be used like any other type and declared anywhere in the code. The main function of this syntax is to define custom aliases for commonly used types, making the code easier to read and understand.

What are the syntax and structure characteristics of lambda expressions? What are the syntax and structure characteristics of lambda expressions? Apr 25, 2024 pm 01:12 PM

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

Parent class calling syntax in PHP8.0 Parent class calling syntax in PHP8.0 May 14, 2023 pm 01:00 PM

PHP is a server-side scripting language widely used in Web development, and PHP8.0 version introduces a new parent class calling syntax to make object-oriented programming more convenient and concise. In PHP, we can create a parent class and one or more subclasses through inheritance. Subclasses can inherit the properties and methods of the parent class, and can modify or extend their functionality by overriding the methods of the parent class. In ordinary PHP inheritance, if we want to call the method of the parent class in the subclass, we need to use the parent keyword to refer to the parent

The connection and difference between Go language and JS The connection and difference between Go language and JS Mar 29, 2024 am 11:15 AM

The connection and difference between Go language and JS Go language (also known as Golang) and JavaScript (JS) are currently popular programming languages. They are related in some aspects and have obvious differences in other aspects. This article will explore the connections and differences between the Go language and JavaScript, and provide specific code examples to help readers better understand these two programming languages. Connection: Both Go language and JavaScript are cross-platform and can run on different operating systems.

What is the difference between C and C++? What is the difference between C and C++? Aug 29, 2023 pm 11:53 PM

The C programming language C is a general-purpose, high-level language originally developed by Dennis M. Ritchie at Bell Labs to develop the UNIX operating system. C was first implemented in 1972 on the DECPDP-11 computer. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, C compiler, and almost all UNIX applications are written in C. For various reasons, C language has now become a widely used professional language. It is a structured language that is easy to learn, it produces efficient programs, it can handle low-level activities, and it can run on a variety of computers.

Understand the basic units of C language Understand the basic units of C language Mar 21, 2024 pm 05:36 PM

C language is a programming language widely used in system programming and application software development. Its basic units mainly include variables, data types, operators, etc. When learning and understanding the basics of C language, mastering these basic units is particularly critical. This article will introduce the basic units of C language through specific code examples to help readers better understand. First, let's take a look at variables in C language. Variables are used to store data in C language. Each variable has its own data type and can store different types of data, such as integers and floating point.

The usage and syntax of exponentiation operation in C language The usage and syntax of exponentiation operation in C language Feb 18, 2024 pm 04:05 PM

Introduction to the syntax and usage of power operation in C language: In C language, power operation (poweroperation) is a common mathematical operation, which is used to calculate the power of a number. In C language, we can use standard library functions or custom functions to implement exponentiation operations. This article will introduce the syntax and usage of exponentiation operation in C language in detail, and provide specific code examples. 1. Use the pow() function in math.h. In C language, the pow() function is provided in the math.h standard library for executing

See all articles