詳細(xì)知道Mysql的基礎(chǔ)設(shè)置
發(fā)表時(shí)間:2023-09-04 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]一、表復(fù)制1、復(fù)制表結(jié)構(gòu) 將表hello的結(jié)構(gòu)復(fù)制一份為表hello32、復(fù)制數(shù)據(jù)a、如果兩張表的結(jié)構(gòu)一樣且你要復(fù)制所有列的數(shù)據(jù)mysql> insert into hello3 select * from hello;b、兩張表結(jié)可能不一樣且你只要復(fù)制部分列的數(shù)據(jù)mysql> ...
一、表復(fù)制
1、復(fù)制表結(jié)構(gòu) 將表hello的結(jié)構(gòu)復(fù)制一份為表hello3

2、復(fù)制數(shù)據(jù)
a、如果兩張表的結(jié)構(gòu)一樣且你要復(fù)制所有列的數(shù)據(jù)
mysql> insert into hello3 select * from hello;
b、兩張表結(jié)可能不一樣且你只要復(fù)制部分列的數(shù)據(jù)
mysql> insert into hello3 (name,sex,degree) select name,sex,degree from hello;
二、索引
1、create 創(chuàng)建索引(只能創(chuàng)建普通索引和唯一索引)
創(chuàng)建普通索引:mysql> create index in_name on hello(name); 給表hello中的name列創(chuàng)建名為 in_name的索引。
創(chuàng)建唯一索引:mysql> create unique index un_name on hello(name); 給表hello中的name列創(chuàng)建唯一索引名為 un_name的索引。
查看索引:mysql> show index from hello; 查看表 hello的索引。
刪除索引:mysql> drop index in_name on hello; 刪除hello表中名為in_name的索引。
2、alter 創(chuàng)建索引(創(chuàng)建索引的通用方式)
創(chuàng)建普通索引:mysql> alter table hello add index in_name(name); 給表hello中的name列創(chuàng)建名為 in_name的索引。
刪除(普通/唯一)索引:mysql> alter table hello drop index in_name; 刪除表hello中名為in_name的普通索引。
刪除自增:mysql> alter table hello modify id int unsigned not null;刪除表hello中 id列(int類型) 的自增特性。
刪除主鍵索引:mysql> alter table hello drop PRIMARY KEY;
創(chuàng)建唯一索引:mysql> alter table hello add unique(name); 給hello表的 name創(chuàng)建唯一索引 索引名是默認(rèn)的。
創(chuàng)建主鍵索引:mysql> alter table hello add primary key(id); 給hello表的id字段創(chuàng)建主鍵索引。
將主鍵索引設(shè)置為自增:mysql> alter table hello modify id int unsigned not null auto_increment;將hello表中的主鍵id列設(shè)置為自增。
三、視圖
主表數(shù)據(jù)的變化,視圖會(huì)時(shí)時(shí)做相應(yīng)的變化。如果視圖所依賴的表出現(xiàn)錯(cuò)誤(被刪除)則視圖也會(huì)發(fā)生錯(cuò)誤。
創(chuàng)建視圖:mysql> create view v_hello as select * from hello where id >5;
刪除視圖:mysql> drop view v_hello;
查看視圖的創(chuàng)建過(guò)程:mysql> show create view v_hello; 查看視圖v_hello 的創(chuàng)建過(guò)程。
字符函數(shù)
1、CONCAT(str1,str2,....) 字符鏈接函數(shù)
mysql> select concat('A','B' );
2、LCASE(str1) 轉(zhuǎn)為小寫
mysql> select lcase("MYSQL");
3、UCASE(str1) 轉(zhuǎn)大寫
mysql> select UCASE("Mysql");
4、LENGTH(str) str的長(zhǎng)度
mysql> select length('mysql');
5、LTRIM(Str) 去除前段空格
mysql> select LTRIM(' mysql');
6、RTRIM(str) 去除后端空格
mysql> select RTRIM(' mysql ');
7、REPEAT(str,count) 重復(fù)count次
mysql> select repeat('mysql',2);
8、REPLACE(str,search_str,replcae_str) 將str中的search_str 替換為replac_str
mysql> select REPLACE('mysql','m','M');
9、SUBSTRING(str,postion,length) 從str的postion開(kāi)始取length個(gè)字符
mysql> select substring('mysql',1,2); 從1開(kāi)始
10、SPACE(count) 生成count個(gè)空格
mysql> select concat(space(3),'mysql');
數(shù)學(xué)函數(shù)
1、BIN(decimal number): 將十進(jìn)制轉(zhuǎn)二進(jìn)制
2、CEILING(number) 向上取整 mysql> select ceiling(10.12); 結(jié)果:11
3、FLOOR(number) 向下取整 mysql> select ceiling(10.12); 結(jié)果:10
4、MAX(column) 獲取 最大列
5、MIN(column) 獲取最小列
6、SQRT(num) 開(kāi)平方
7、RAND() 返回0-1之間的隨機(jī)數(shù)值
日期函數(shù)
1、CURDATE() 返回當(dāng)前日期格式 yyyy-MM-dd
2、CURTIME()返回檔期時(shí)間 12:11:56
3、NOW()返回當(dāng)前時(shí)間 2017-05-12 21:12:34
4、UNIX_TIMESTAMP(date) 返回當(dāng)前date的時(shí)間戳
5、FROM_UNIXTIME()返回UNIX時(shí)間戳的日期值
6、WEEK(date)返回當(dāng)前時(shí)間date為一年中的第幾周
7、YEAR(data)返回當(dāng)前時(shí)間date的年份
8、DATEDIFF(expr1,expr2) 返回expr1與expr2之間的天數(shù)
五、預(yù)處理語(yǔ)句
無(wú)變量:
創(chuàng)建預(yù)處理語(yǔ)句:mysql> prepare stmt1 from 'select * from hello where id>5';創(chuàng)建一個(gè)名為stmt1的預(yù)處理語(yǔ)句
執(zhí)行預(yù)處理語(yǔ)句:mysql> execute stmt1;執(zhí)行stmt1預(yù)處理語(yǔ)句
帶變量:
創(chuàng)建帶參數(shù)的預(yù)處理語(yǔ)句:mysql> prepare stmt1 from 'select * from hello where id>?'
設(shè)置變量:mysql> set @i=6;
執(zhí)行預(yù)處理語(yǔ)句:mysql> execute stmt2 using @i;
刪除預(yù)處理語(yǔ)句:mysql> drop prepare stmt2; #mysql> DEALLOCATE PREPARE stmt2;
注意:每一次執(zhí)行完EXECUTE時(shí),養(yǎng)成好習(xí)慣,須執(zhí)行DEALLOCATE PREPARE … 語(yǔ)句,這樣可以釋放執(zhí)行中使用的所有數(shù)據(jù)庫(kù)資源(如游標(biāo))。
不僅如此,如果一個(gè)session的預(yù)處理語(yǔ)句過(guò)多,可能會(huì)達(dá)到max_prepared_stmt_count的上限值。
六、事務(wù)處理
mysql默認(rèn)事務(wù)是自動(dòng)提交的。在做mysql事務(wù)處理時(shí)請(qǐng)將數(shù)據(jù)庫(kù)或者表的ENGINE 設(shè)置為InnoDB
將表的存儲(chǔ)引擎設(shè)置為INNODB:mysql> alter table hello engine=innodb;
設(shè)置mysql為非自動(dòng)提交:mysql> set autocommit=0;
產(chǎn)生事務(wù):mysql> delete from hello where id>7;
事務(wù)回滾:mysql> rollback;
事務(wù)提交:mysql> commit;
關(guān)于事務(wù)中的還原點(diǎn):
創(chuàng)建一個(gè)事務(wù):mysql> insert into hello (sex,degree,name) values(1,12312.32,'HHH');
對(duì)該事務(wù)設(shè)置還原點(diǎn):mysql> savepoint p1;
回滾到指定的還原點(diǎn):mysql> rollback to p1; 此時(shí)事務(wù)恢復(fù)到p1,也就是p1之后的事務(wù)p2 ,p3..這些還原點(diǎn)將失效。
回滾到原始的還原點(diǎn):mysql> rollback;
七、存儲(chǔ)過(guò)程
<!-- 創(chuàng)建存儲(chǔ)過(guò)程 hello1()-->
CREATE PROCEDURE hello1()
BEGIN
SET @i=0;
WHILE @i<100 DO
insert INTO hello (sex,degree,name) VALUES(1,@i,CONCAT('name',@i));
SET @i=@i+1;
END WHILE;
end;
<!-- 查看存儲(chǔ)-->
SHOW PROCEDURE STATUS;
<!-- 查看hello1()存儲(chǔ)過(guò)程-->
show CREATE PROCEDURE hello1;
<!-- 執(zhí)行存儲(chǔ)過(guò)程-->
CALL hello1;
八、重排auto_increment值
mysql中我們的主鍵id如果設(shè)置為主鍵自增策略,那我們?nèi)绾吻蹇毡恚⑶一謴?fù)自增列id的值。
方式一:使用truncate table tableName; 該方式在清空表的同時(shí)恢復(fù)auto_increment 的值。
方式二:
1、mysql> delete from hello3; 清空表 (該方式效率較低)
2、mysql> alter table hello3 auto_increment=1; 恢復(fù)auto_increment 的起始值為1
以上就是詳細(xì)了解Mysql的基礎(chǔ)操作的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門到精通的SQL知識(shí)。