MySQL优化之简单语法_MySQL
1、默认约束
--mysql CREATE TABLE emp ( id INT DEFAULT 12 )
2、设置自增列
MYSQL的自增列一定要是有索引的列,设置种子值要在表的后面设置--mysql -- 设置自增ID从N开始 CREATE TABLE emp ( ID INT PRIMARY KEY AUTO_INCREMENT ) AUTO_INCREMENT = 100 ; --(设置自增ID从100开始)
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%'; -- 全局变量
之后再插入一条数据,那么这条数据的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 针对任何表
一般系统定义的全局变量都是以@@开头,用户自定义变量以@开头。
使用@@identity的前提是在进行insert操作后,执行select @@identity的时候连接没有关闭,否则得到的将是NULL值。
(4) SHOW TABLE STATUS LIKE 'person'
得出的结果里边对应表名记录中有个Auto_increment字段,里边有下一个自增ID的数值就是当前该表的最大自增ID.
3、查看表定义
DESC emp
4、修改表名
ALTER TABLE emp RENAME emp2
5、修改字段的数据类型
将id字段的int类型改为bigintALTER TABLE emp2 MODIFY id BIGINT
6、修改字段名
MYSQL里修改字段名的时候需要加上字段的数据类型否则会报错,而CHANGE也可以只修改数据类型,实现和MODIFY同样的效果,方法是将SQL语句中的“新字段名”和“旧字段名”设置为相同的名称,只改变“数据类型”,改变数据类型,例如刚才那个例子,将id列改为bigint数据类型ALTER TABLE emp2 CHANGE id id BIGINT
7、添加字段
ALTER TABLE emp2 ADD NAME NVARCHAR(200) NULL
8、删除字段
MYSQL删除字段不需要添加COLUMN关键字的ALTER TABLE emp2 DROP NAME
9、删除外键约束
如果是外键约束,需要使用 DROP FOREIGN KEY,如果是主键约束需要使用DROP PRIMARY KEY--删除外键约束
ALTER TABLE emp2 DROP FOREIGN KEY fk_emp_dept
ALTER TABLE emp2 DROP PRIMARY KEY pk_emp_dept
10、删除表
DROP TABLE emp2
DROP TABLE IF EXISTS emp1 ,emp2
补充:
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;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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.

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.

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 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.

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.

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.

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
