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
mySqlStringTypesimpactStorageAndPerformanCeaseAsfollows:1)長度,始終使用theSamestoragespace,whatcanbefasterbutlessspace-felfficity.2)varCharisvariable varcharisvariable length,morespace-morespace-morespace-effficitybuteftife buteftife butfority butfority textifforlyslower.3)

mysqlStringTypesIncludeVarChar,文本,char,Enum和set.1)varCharisVersAtileForvariable-lengthStringStringSuptoPuptOuptoPepePecifiedLimit.2)textisidealforlargetStortStorStoverStoverStorageWithoutAutAdefinedLength.3)charlisfixed-lenftenge,for forConsistentDatalikeCodes.4)

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)

toadduserInmysqleffect和securly,跟隨台詞:1)USEtheCreateUserStattoDaneWuser,指定thehostandastrongpassword.2)GrantNecterAryAryaryPrivilegesSustherthing privilegesgeStatement,usifementStatement,adheringtotheprinciplelastprefilegege.3)

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

MySQL中的字符串數據類型包括CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT,排序規則(Collations)決定了字符串的比較和排序方式。 1.CHAR適合固定長度字符串,VARCHAR適合可變長度字符串。 2.BINARY和VARBINARY用於二進制數據,BLOB和TEXT用於大對像數據。 3.排序規則如utf8mb4_unicode_ci忽略大小寫,適合用戶名;utf8mb4_bin區分大小寫,適合需要精確比較的字段。

最佳的MySQLVARCHAR列長度選擇應基於數據分析、考慮未來增長、評估性能影響及字符集需求。 1)分析數據以確定典型長度;2)預留未來擴展空間;3)注意大長度對性能的影響;4)考慮字符集對存儲的影響。通過這些步驟,可以優化數據庫的效率和擴展性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6
視覺化網頁開發工具