bitsCN.com
MySQL高级操作(上)
一、MySQL表复制
create table t2 like t1; #复制表结构,t2可以学习到t1所有的表结构
insert into t2 select * from t1; #复制表数据,但是这样还是会有缺陷,因为没有考虑到列的对应,因为t1与t2的表结构完全一致,所以此次操作才不会出错!
建议:
insert into t3(name) select name from t1; #指定复制的列
二、MySQL索引
1、直接创建索引
create index index_name on table_name(column_list); #创建普通索引
create unique index index_name on table_name(colume_list); #创建唯一索引,请在创建唯一索引之前确保该列没有重复值,不然,创建不成功!
2、直接删除索引
drop index index_name on table_name;
3、修改-创建索引
alter table table_name add index [index_name](colum_list); #创建普通索引
alter table table_name add unique [index_name](column_list); #创建唯一索引
alter table table_name add primary key [index_name](column_list); #创建主键索引,如果不添加index_name,则使用column_list作为默认索引名
4、修改-删除索引
alter table table_name drop index index_name; #删除普通/唯一索引
alter table table_name drop primary key; #删除主键索引
【推荐使用方式3、4】
【附】
1、查看索引:show index from t1 /G
2、alter table table_name modify id int not null;
三、MySQL视图
视图:通过一个条件,把一部分数据从一张表里面提取出来,形成一张中间表,这张表就是视图
注意:视图随着主表的改变而改变
1、创建视图
create view view_name as select *from table_naem where id > 4 and id
3、查看创建了哪些视图
showtables; #视图就是一个中间表
3、查看视图中数据
select* from view_name; #与查看表数据相同
4、删除视图
drop view view_name;
四、MySQL内置函数补充
查看函数作用及简单示例:? function_name
e.g. ? lcase;
1、字符串函数
1)lcase(“string”)/ucase(“string”) #转换成小写/大写,与lower(str)/upper(str)作用相同
2)length(“string”) #返回字符串的长度
3)repeat(“string”,n) #将字符从重复n次
4)space(n) #生成n个空格
2、数学函数
1)bin(decimal_number) #十进制转二进制
2)ceiling(n) #作用与ceil相同,向下取整
3)sqrt(n) #开平方
4)max(col)/min(col) #取最大/最小值,聚合时使用
5)rand() #生成随机数
select * from table_name order by rand(); #使用rand函数作为排序基准
3、日期函数
1)datediff(expr1,expr2) #返回expr1和expr2相差的天数,如果expr1> expr2,则返回正值
bitsCN.com
MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


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

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
