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
mysqloffersvariouscharactersetsforstringdatatypes:1)latin1 forwesterneuropeanlanguages,2)utf8 formultingualsupport,3)utf8mb4f OREXTEDENDENDENENICODECLUDINGEMOJIS,4)UCS2FORIXED-WIDTHENCODING,5)assiiforbasiclatin.ChoosideStherightStetSetensensersdaintegrity

流式传输BLOB确实比直接存储更好,因为它能减少内存使用和提高性能。1)通过逐步读取和处理文件,避免了数据库膨胀和性能下降。2)流式传输需要更复杂的代码逻辑,且可能增加I/O操作次数。

mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)长度,始终使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

mySqlStringTypesIncludeVarChar,文本,char,enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptOptoPeptoPepecifientlimit.2)textisidealforlargetStortStorStoverStorextorewiteWithoutAdefinedLengthl.3)charlisfixed-Length

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)

toadduserInmysqleffectection andsecrely,theTheSepsps:1)USEtheCreateuserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNectalRevileSaryPrivilegesSustate,usiveleanttatement,AdheringTotheTeprinciplelastPrevilegege.3)

toaddanewuserwithcomplexpermissionsinmysql,loldtheSesteps:1)创建eTheEserWithCreateuser'newuser'newuser'@''localhost'Indedify'pa ssword';。2)GrantreadAccesstoalltablesin'mydatabase'withGrantSelectOnMyDatabase.to'newuser'@'localhost';。3)GrantWriteAccessto'


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

记事本++7.3.1
好用且免费的代码编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境