1.表復(fù)制
mysql復(fù)制表的兩種方式。
第一浆劲、只復(fù)制表結(jié)構(gòu)到新表
create table 新表 select * from 舊表 where 1=2
或者
create table 新表 like 舊表
第二、復(fù)制表結(jié)構(gòu)及數(shù)據(jù)到新表
create table 新表 select * from 舊表
只復(fù)制指定的字段止邮,只要將上面的*改為具體的字段即可
將表數(shù)據(jù)復(fù)制到新表中
insert into 新表 select * from 舊表
將新建的字段改名
create table newtable as
(select username as uname,password pwd from user)
復(fù)制表的同時新增表字段
create table newtable
(id int not null auto_increment primary key)
as
(select * from oldtable)
2.常用函數(shù)
lcase(str)或lower(str) 返回將字符串str中所有字符改變?yōu)樾懞蟮慕Y(jié)果
ucase(str)upper(str) 返回將字符串str中所有字符轉(zhuǎn)變?yōu)榇髮懞蟮慕Y(jié)果
left(str,x)返回字符串str中最左邊的x個字符
right(str,x) 返回字符串str中最右邊的x個字符
length(s)返回字符串str中的字符數(shù)
ltrim(str) 從字符串str中切掉開頭的空格
rtrim(str) 從字符串str中切掉結(jié)尾的空格
trim(str):去掉首尾空格
position(substr,str) 返回子串substr在字符串str中第一次出現(xiàn)的位置
reverse(str) 返回顛倒字符串str的結(jié)果
group_concat(字段名)放置每一組的某字段的值的集合
status; 查看當(dāng)前mysql各種參數(shù)
select version();查看當(dāng)前版本
select user();查看當(dāng)前用戶
select database();查看當(dāng)前使用的數(shù)據(jù)庫
select curdate();當(dāng)前日期
select curtime();當(dāng)前時間
select now();當(dāng)前日期時間
show grants;查看當(dāng)前用戶自己權(quán)限
show grants for dba@localhost; 查看指定用戶權(quán)限
3.修改數(shù)據(jù)庫編碼
alter database test999 character set utf8;
注意:==在MySQL中所有的UTF-8編碼都不能使用中間的“-”温技,即UTF-8要書寫為UTF8。==
4.字段操作
alter table stu modify gender char(2); #修改類型
alter table stu change gender sex char(2);#更名
alter table stu drop gender;#刪除
alter table stu rename to student;#修改表名
5.創(chuàng)建用戶
create user 用戶名@地址 identified by '密碼';
create user testuser1@'%' identified by '123';
6.修改權(quán)限
-- []表示可選
grant 權(quán)限名稱 on 數(shù)據(jù)庫 to 賬戶@主機(jī) [[identified by 密碼] with grant option]
grant create,alter,drop,insert,update,delete,select on test999.* to testuser1@localhost;
grant all on test999.* to testuser1@localhost;
注意:==修改權(quán)限后必須刷新權(quán)限==
flush privileges;
==with grant option==:表示該用戶可以將自己擁有的權(quán)限授權(quán)給別人
7.撤銷權(quán)限
-- revoke 跟 grant 的語法差不多廊移,只需要把關(guān)鍵字 “to” 換成 “from” 即可
revoke all on *.* from dba@localhost;
8.刪除賬號
drop user '用戶名'@'主機(jī)';
例:
drop user 'laowang'@'%';
delete from user where user='用戶名';
例:
delete from user where user='laowang';
-- 操作結(jié)束之后需要刷新權(quán)限
flush privileges
9.修改密碼
方法一:
use mysql;
update user set password=password('123') where user=用戶 and host='localhost'
flush privileges;
方法二:
set password for root@localhost = password('123');
flush privileges;
10.分頁
select * from 表名 limit start,count
從start開始糕簿,獲取count條數(shù)據(jù)
求第n頁的數(shù)據(jù)
select * from students where limit (n-1)*m,m
11.子查詢
1.標(biāo)量子查詢
select * from students where age > (select avg(age) from students);
2.列級子查詢
select name from classes where id in (select cls_id from students);
3.行級子查詢
select * from students where (height,age) = (select max(height),max(age) from students);
4.子查詢中特定關(guān)鍵字使用
in 范圍
- 格式: 主查詢 where 條件 in (列子查詢)
三范式
N范式必須滿足(N-1)范式
1NF:字段原子性
==字段原子性探入,字段不可再分割。==
2NF:消除對主鍵的部分依賴
==確保表中的每列都和主鍵相關(guān)==
依賴:A字段可以確定B字段懂诗,則B字段依賴A字段
即在表中加上一個與業(yè)務(wù)邏輯無關(guān)的字段作為主鍵
對主鍵的部分依賴:某個字段依賴復(fù)合主鍵中的一部分蜂嗽。
解決方案:新增一個獨(dú)立字段作為主鍵。
3NF:消除對主鍵的傳遞依賴
==確保數(shù)據(jù)表中每列數(shù)據(jù)都和主鍵直接相關(guān)殃恒,而不能間接相關(guān)==
傳遞依賴:A->B,B->C A->C