MySQL-學習筆記

命令行操作數據庫:
terminal:
/Applications/XAMPP/bin
./mysql -u root -p
輸入密碼

創建數據庫
create database database_name;
create database database_name if not exist;
切換數據庫
use database_name;
刪除數據庫
drop database database_name;
#重命名數據庫:數據庫的重命名需要導出數據庫,新建數據庫,然後再重命名
顯示數據庫列表:
show database;

創建表:
create table tbl_user(id int(3), name varchar(8), password varchar(20));
重命名錶:
alter table tbl_user rename to tbl_members;
顯示數據庫中的所有表:
show tables;
show tables from database_name;
顯示錶結構:
describe table_name;
添加字段:
alter table tbl_user add email varchar(255) not null;
修改字段:
alter table tbl_user change id newid int(3);
刪除字段:
alter table tbl_user drop email;

數據庫文件列表解析:
*.opt —>數據庫的配置信息
*.frm —>表結構信息
*.lbd —>數據記錄,表的索引信息

數據類型—>保證數據的存儲空間和效率;

整數類型:tinyint(1字節), smallint(2字節), mediumint(3字節), int(4字節) , bigint(8字節)
alter table tabledata add intsix int(6) zerofill; —>int(6) 6表示顯示的位數,zerofill表示空位數用0填充,默認用空格代替

浮點類型:float, double, decimal
alter table tabledata add float2 float(5,2); —>5表示總共有5位,2表示小數點後2位

Bool類型:
tinyint —> true==1, false==0

字符類型:char, varchar(可變長度), blob(區分大小寫), text(一般大文本存儲), tinytext, longblob…
create table table_string(charstring char(10)); —> char(10) 表示長度不能超過10,如果超過數據會被截斷
create table table_string(charstring varchar(10)); —> varchar(10) 表示默認長度爲10,如果超過位數會自動延長
alter table table_string change charstring charstring char(10) binary; —> 將charstring字段修改成二進制存儲,使有大小寫區分;
Enum:
alter table table_string add gender enum(‘m’,’n’); —> 只能插入括號內字符,否則會出錯
Set:
alter table table_string add setcol set(‘A’,’B’,’CD’); —>可以插入多個字符

日期和時間類型:date(YYYY-MM-DD), time(HH:MM:SS), year(YYYY), datetime, timestamp;
create table tabledata(birthday date);
insert into tabledata values(‘2015-04-01’)(20150401) —> 插入字符或者數值都可以
alter table tabledata add datetimecol2 datetime(2); —>2表示秒以後的位數
alter table tabledata add time1 timestamp; —> 第一個timestamp默認插入當前時間,第二個timestamp默認插入0;

插入數據:
insert into tabledata[] values();

連接到數據庫的三種方式:
1)shell方式 —>terminal終端
/Applicatioin/XAMPP/Bin/mysql -h localhost -u Root -p
mysql —help
2)MySQL Workbench —>client
3)phpMyAdmin —>web

算術運算符
select 1+2; =3
select 2+null; =null
select ‘2’ *’5’ ; =10 —>字符會自動轉換爲數字,不能轉換的字符會用0代替
select sint - intsix from tabledata; —> 字段與字段相減

比較運算符
= <> != > <=> between in is null like regexp

select 1=2; =0(false)
select 1=null; =null —>不能比較
select 1<=>null; =0(false) —> 對null類型的安全比較
select binary ‘jike’ = ‘JIKE’; =0(false) —>區分大小寫
select ‘jike’ = ‘JIKE’; =1(true) —>不區分大小寫
select ‘i’ > ‘k’ ; =0(false) —>按字母表去比較
select ‘b’ between ‘a’ and ‘c’; —>?
select not ‘d’ between ‘a’ and ‘c’;
select 5 in(2,3,5,6); =1(true)
select ‘jikexueyuan’ like ‘jike%’; =1(true) —>%表示多個字符
select ‘jikexueyuan’ like ‘ike’; =1(true) —>表示單個字符的匹配

邏輯運算符:
not/! and/&& or/|| xor
select (4>3) xor (3>5); =1(true);

位運算符:
& | ^ ~ >> <<
select 9 | 4; =13 —>9變成2進制,然後取或運算
運算的優先級應該用()保證運算順序

基本的插入數據
create table addressbook(
fname varchar(255) null);
decribe addressbook;
insert into addressbook (fname) values(‘xxx’);
create table forums(name varchar(150) not null,
category varchar(50) default ‘UNIX’ not null,
postsperpage smallint default 15 not null));

自動遞增:
create table users(uid tinyint not null auto_increment,
uname varchar(50) not nul default ‘’,
primary key(uid));
唯一性:
alter table users add uique(uname);

更新數據和刪除數據:
update users set uname = ‘jike’ where uid = 2;
delete from users where uid = 1;
delete from users; —>默認不能直接刪除整個表,必須要關閉安全模式纔可以直接刪除
show variables like ‘%sql_safe%’;
set sql_safe_updates = 0; —>關閉安全模式
insert into users(uname) values (‘hey’);

truncate table users; —> 清空數據表,自增鍵會重置
delete from users; —>刪除數據表,自增鍵不會重置

數學函數:
ABS(-5.8); =5.8 —>取絕對值
ceiling(5.8); =6 —>得到比這個數大的整數
floor(5.8); =5 —>得到比這個數小的整數
greatest(1,2,3,8,12); =12 —>取最大值
least(1,2,3,8,12); =1 —>取最小值
mod(2,3); =2 —>取餘數
pi(); =3.141593 —>取圓周率
rand(2); —>隨機數
round(10.2364,2); —>2表示保留兩位小數
truncate(10.2364,2); —>從2位小數截斷
sign(-5.4); —>判斷正負
sin(1); —>正弦
cos(1); —>餘弦
degree(1); —>弧度轉換爲角度
radian(57); —>角度變成弧度
pow(2,3); —>2的3次方=8;
exp(3); —>?
sqrt(4); —>4的平方根=2
log(8); —>對數
bin(10); —>10進制轉換成2進制

聚合函數:
avg(字段) from table_test; —>平均數據;
min()
max()
count()
std —>標準偏差
variance —>標準方差

字符函數:
length(‘hello’); =5 —>字符串的長度
lcase(‘hello’); —>轉換爲小寫
lower(‘hello’); —>轉換爲小寫
ucase(‘hello’); —>轉換爲大寫
upper(‘hello’); —>轉換爲大寫
strcmp(‘hello’,’yes’); —>比較字符串
position(‘yes’ in ‘hyesman’); —>查找
replace(‘yes’,’y’,’hh’); —>替換y—>hh
(‘yes’,2,1,’hh’);
concat(‘hello’,’world’); —>合併字符串
concat_ws(‘hello’,’world’); —>
left(‘helloworld’,3); —>從左邊開始,取3位字符
right() —>從右邊開始,取n位字符
lpad() —>從左邊開始填充字符
rpad() —>從右邊開始填充字符
trim() —>去掉空格
substring (‘macket’,2,3); —>第2位開始取,取3位
ASCII(‘a’) —>取ASCII碼

時間與日期函數:
now(); —>現在的日期和時間
curtime(); —>現在的時間
curdate(); —>今天的日期
year(20030312); —>取年份
mouth(20030312); —>取月份
mouthname();
dayofyear();
dayofweek();
dayname();
week();
hour(’14:56’); —>取小時
second(); —>取秒
minute(); —>取分鐘
date_add(now(),interval 3 mouth) —>加3個月
date_sub(now(),interval 3 day) —>減3天

數據加密函數:
select password(’secret’); —>加密(不可逆)
select encrypt(’secret’,’abc’); —>unix系統的加密函數
select encode(’secret’,’abc’); —>加密
select decode(‘uname’,’abc’); —>解密
aes_encrypt(’secret’,’abc’);
aes_decrypt(uname,’abc’) from users;
select md5(’secret’); —>查詢md5值
select sha(’secret’); —>查詢md5值

控制流函數:
select if(1>10,2,3); —>如果true = 2,如果false = 3;
select ifnull(1,2); —>true=2,false=1;
nullif(1,2);
case ‘green’ when ‘red’ then xxx

格式化:
date_format(now(),%w,%d,%m,%y,%r);
time_format(‘100:21:12’,’%h:%i %p’);
inet_aton(‘192.168.210.191’); —> ip地址轉成數字
inet_ntoa(‘number’); —>數字轉成ip
cast(’99’ as signed); —>?
convert(’23’,signed);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章