1. 程式人生 > >MySQL操作指令與內建函式總結

MySQL操作指令與內建函式總結

MySQL常用操作指令和內建函式,歡迎留言補充!

  1. 建庫,選擇庫
create database class1824; 
use python1804;

  1. 刪庫
drop database class1824;

3.建表,刪表

create table user(userid int(11),username varchar(32),password char(32)); 
drop table user;
  1. 改表名
alter table user  rename users;
  1. 表字段改名
alter table user change userid id int(11);
  1. 修改欄位型別與位置
alter table user modify username char(32) first;
  1. 新增刪除表字段
alter table user add column password char(64) after username;
alter table user drop column sex;
  1. create建立索引,create 不能建立主鍵索引,注意與alter的區別
create index in_id on t1(id);
  1. alter建立索引,create 不能建立主鍵索引,注意與alter的區別
alter table users add unique(欄位名 ); 
#fulltext(欄位名 );primary key(欄位名 );index(欄位名)
  1. 刪除索引
alter table users drop index username on t1;
  1. 刪除主鍵索引,先取消遞增
alter table users modify id int(11) unsigned not null;
alter table users drop primary key; 
  1. 插入資料方法一,所有欄位,順序一致
insert into users values(1,"haha","adfdsfadsf12"),(2,"xixi","afdsf123321");
  1. 插入資料方法二,設定了遞增的欄位可不操作
insert into users(id,username,password) values(3,"hehe","12333adf123");
  1. 替換列資料
update scores set nu=replace(nu,1,2);
  1. 去重
select distinct balance from money;
  1. 排序,desc降序,asc升序。
select * from money where age>30 order by balance desc,age asc limit 2 ,5;
asc升序;

17.分組,group by

select count(province),province from money group by province;

18.條件篩選group by

select count(province) as result,province from money group by province with rollup;

19.條件篩選group by

select count(province) as result,province from money group by province having result>1;

20.表聯合查詢

select user.username as '使用者名稱',order_goods.oid,order_goods.name as "商品名" from user,order_goods where user.uid=order_goods.uid;

21.表聯合查詢2

select user.username as '使用者',order_goods.name as "商品" from user inner join order_goods on user.uid=order_goods.uid;

22.left join 語法

select * from user left join order_goods on user.uid=order_goods.uid; 

23.注意與22的去區別

select * from user where uid in(select uid from order_goods);
select uid from user union all select uid from order_goods;

24.更新資料update

update user set username="哈哈",password="123456" where uid=8;

25.同時對兩個表更新

update money m,users u set m.balance=m.balance*u.id,u.password=m.balance where m.id=u.id;

26.delete from,累加id從原來的基礎上累加,如1,2,3,將id=2刪除了,id=3不變!注意與27中truncate的區別

delete from money where id>9; 
delete from  表名; 
  1. truncate清空表以後 id 從1 開始
truncate table users; 

28.資料庫遠端連線,注意符號*的使用

grant 許可權 on 庫名.表名 to '使用者名稱'@'ip地址' identified by 密碼;

grant all on *.* to 'root'@'localhost' identified by '123456' with grant option; 
grant select,insert on class1824.* to 'jinxingping'@'localhost' identified by '123321';
flush privileges;
service mysql restart;

29.刪除指定許可權

revoke insert on class1824.* from 'jinxingping'@'localhost';  

30.複製表結構和資料

create table user1 like users;
insert into user1 select * from users;

31.表檢視 view

create view v_t1 as select * from t1 where id>2 and id<8; show create/drop view v_t1;
  1. mysql常用內建函式
mysql 顯示內容 用 select  
字串函式  
mysql> select concat("hello","world") as nihao; #拼接字串
select lcase("MYSQL");#轉小寫
select ucase("mysql"); #轉大寫
mysql> select length("haha"); 字串的長度
mysql> select ltrim("    haha"); 去除左側空格
+-------------------+
| ltrim("    haha") |
+-------------------+
| haha              |
+-------------------+
1 row in set (0.00 sec)

mysql> select rtrim("    haha      ");去除右側空格
mysql> select repeat("重要的事情說三遍",6);
select replace("java 是世界上最好的語言","java","python");
mysql> select substr("python is so good",2,7); 從第二個字元開始  擷取7個  mysql 從1開始 
mysql> select concat(space(20),'haha'); #生成20個空格   
數學函式  
mysql> select ceiling(123.1);
mysql> select floor(123.1);
mysql> select rand(); 求隨機數  0-1之間的小數 
mysql> select bin(10);


日期函式
mysql> select curdate(); 返回日期  2018-7-25
mysql> select curtime(); 11:21:33
mysql> select now(); 日期+時間  2018-07-25 11:22:14  
mysql> select week(now()); 返回29 表示當前第幾周   
mysql> select year(now()); 返回2018 
mysql> select unix_timestamp(now()); 當前時間的時間戳 
mysql> select from_unixtime(1532489069);
+---------------------------+
| from_unixtime(1532489069) |
+---------------------------+
| 2018-07-25 11:24:29       |
+---------------------------+ 將當前的時間戳格式化為日期