mysql、sqlserver、oracle三种数据库维护索引、外键、字段语法总结_MySQL
mysql、sqlserver、oracle三种数据库维护索引、外键、字段语法总结
1. MYSQL数据库
1) 创建索引
CREATE INDEX index_name ON table_name(column_list)
CREATE UNIQUE INDEX index_name ON table_name(column_list)
修改表的方式添加索引
ALTER TABLE table_name ADD INDEX index_name(column_list)
ALTER TABLE table_name ADD UNIQUE (column_list)
ALTER TABLE table_name ADD PRIMARY KEY(column_list)
2) 删除索引
DROP INDEX index_name ON talbe_name
修改表的方式删除索引
ALTER TABLE table_name DROP INDEX index_name
ALTER TABLE table_name DROP PRIMARY KEY
3) 创建外键
修改表的方式添加外键
ALTER TABLE TABLE_NAME
ADD [CONSTRAINT 外键名]FOREIGN KEY (index_col_name, ...)
REFERENCES tbl_name (index_col_name, ...)
举例:
ALTER TABLE TABLE_NAME ADD CONSTRAINT FK_NAME(user_id)
REFERENCE sys_user(id)
需要注意两个字段的数据类型一致;
4) 删除外键
ALTER TABLE sys_org DROP FOREIGN KEY fk_s_o_id;
5) 列操作语法
? 添加列 birthday ,有COLUMN关键字
alter table sys_useradd COLUMN birthday char(19) not null;
? 修改列 birthday ,有MODIFY 关键字
alter table sys_usermodify birthday char(10);
? 删除列 birthday,有 DROP COLUMN关键字
alter table sys_userdrop column birthday;
? 修改列名称及属性;把birthday修改成CSRQ ,并改属性为char(10) 和不允许为空not null
alter table sys_userchange birthday CSRQ char(10) not null;
2. SQL SERVER 数据库
1) 创建索引
create UNIQUE INDEX un_index_name on sys_user(user_name);
2) 删除索引
DROP INDEX un_index_name ON sys_user
3) 创建外键
alter table sys_org add CONSTRAINT fk_s_o_id FOREIGN key(create_user)
REFERENCES sys_user(id);
与mysql一致,需要注意两个字段的数据类型一致;
4) 删除外键
ALTER TABLE sys_org DROP constraint fk_s_o_id;
与mysql语法不同
5) 列维护语法
6) 添加列 birthday ,有COLUMN关键字
alter table sys_user add birthday char(19) not null;
7) 修改列 birthday属性 ,有alter column 关键字
alter table sys_user altercolumn birthday char(10);
8) 删除列 birthday,有 DROP COLUMN关键字
alter table sys_user dropcolumn birthday ;
9) 修改列名称及属性;把birthday修改成CSRQ ,并改属性为char(10) 和不允许为空not null;需要分成两步执行;
exec sp_rename 'sys_user.[birthday]','CSRQ','COLUMN';
alter table sys_user altercolumn CSRQ char(10) not null;
3. ORACLE 数据库
1) 创建索引
CREATE INDEX index_name ON table_name(column_list)
CREATE UNIQUE INDEX index_name ON table_name(column_list)
create INDEX ind_s_u_sex on sys_user(sex);
2) 删除索引
DROP INDEX [schema.]indexname;
drop index ind_s_u_sex;
3) 创建外键
修改表的方式添加外键
ALTER TABLE TABLE_NAME
ADD [CONSTRAINT 外键名]FOREIGN KEY (index_col_name, ...)
REFERENCES tbl_name (index_col_name, ...)
举例:
ALTER TABLE TABLE_NAME ADD CONSTRAINT FK_NAME(user_id)
REFERENCE sys_user(id)
需要注意两个字段的数据类型一致;
4) 删除外键
ALTER TABLE TABLE_NAME DROP CONSTRAINT FK_NAME;
举例:
ALTER TABLE sys_org drop constraint fk_s_o_id;
5) 列操作语法
? 添加列 birthday , 有add关键字
alter table sys_useradd birthday char(19) not null;
? 修改列 birthday ,有MODIFY 关键字
alter table sys_usermodify birthday char(10);
? 删除列 birthday,有 DROP COLUMN关键字
ALTER TABLE sys_user DROPCOLUMN birthday;
? 修改列名称及属性;把birthday修改成CSRQ ,并改属性为char(10) 和不允许为空not null;需要分成两步执行;
ALTERTABLE sys_user RENAME COLUMN birthday to CSRQ;
ALTER TABLE sys_user MODIFYCSRQ char(10) ;

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



Oracle index types include: 1. B-Tree index; 2. Bitmap index; 3. Function index; 4. Hash index; 5. Reverse key index; 6. Local index; 7. Global index; 8. Domain index ; 9. Bitmap connection index; 10. Composite index. Detailed introduction: 1. B-Tree index is a self-balancing tree data structure that can efficiently support concurrent operations. In Oracle database, B-Tree index is the most commonly used index type; 2. Bit Graph index is an index type based on bitmap algorithm and so on.

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.

The solutions are: 1. Check whether the index value is correct: first confirm whether your index value exceeds the length range of the array. The index of the array starts from 0, so the maximum index value should be the array length minus 1; 2. Check the loop boundary conditions: If you use the index for array access in a loop, make sure the loop boundary conditions are correct; 3. Initialize the array: Before using an array, make sure that the array has been initialized correctly; 4. Use exception handling: You can use the exception handling mechanism in the program to catch errors where the index exceeds the bounds of the array, and handle it accordingly.

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.

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Introduction: PHP and MySQL are currently the most widely used programming languages and database management systems, and are often used to build web applications and process large amounts of data. Data grouping and data aggregation are common operations when processing large amounts of data, but if indexes are not designed and used appropriately, these operations can become very inefficient. This article will introduce how to use indexes to improve the efficiency of data grouping and data aggregation in PHP and MySQL, and improve

PHP is a scripting language widely used in website development. For developers, it is often necessary to determine whether a field is empty. In PHP, determining whether a field is empty can be achieved through some simple methods. This article will introduce how to determine whether a field is empty in PHP, and provide specific code examples for your reference. In PHP, you can usually use the empty() function or isset() function to determine whether a field is empty. Next, we introduce the usage of these two functions respectively. Use the empty() function

The basic syntax of slicing in Python is to use the [start:end:step] syntax for slicing operations, where start represents the starting position of the slice, end represents the end position of the slice, and step represents the slicing step. If start is omitted, it means slicing from the beginning of the list or string; if end is omitted, it means slicing to the end of the list or string; if step is omitted, it means the step size is 1. For example: my_list=[1,2,3,4,5]#Cut from the 2nd element to the 4th element (excluding the 4th element) sub_list=my_list[1:4]#[2,3,4 ]#Start from the first element until the end of the list sub_li
