1、安裝:安裝後測試,show databases;
創建數據庫:create databases 名; create database if not exists 名;
使用數據庫:use 名;
查看編碼:show variables like '%character%';
修改編碼:alter database 名 character set....;
刪除數據庫:drop database 名;
drop database if exists 名;
創建表:create table 名(id int,name varchar(50));
添加:insert into 名(id,name) values(123,'張三');
查詢:select * from 名;
刪除:delete from 名 where id='123';
修改:update 名 set 列名='值',............where 列名=值
複製表:create table 名 select * from db_test.tb_test;將得到一個tb_test2的tb_test表的一個副本
查看數據庫中所有表:show tables;
查看錶結構:describe 名; desc 名;
刪除表:drop table 名;
添加列:alter table 名 add column 列名 類型;
刪除列:alter table 名 drop 列名;
2、 數據類型:tinyint 默認有符號(-128,127) unsigned 無符號(0,255)
smallint:大整數值 int:整型
float:單精度 bigint:極大整值
double:雙精度 decimal(3,2)三位兩個小數
3、時間和日期型:
Date:YYYY-MM-DD time:HH:MM:SS
year:年份YYYY Datetime:混合YYYY-MM-DD HH-MM-SS
timestamp:YYYYMMDD HHMMSS可以默認 now():當前時間
4、字符串類型:char、varchar、binary、varbinary、blob、text、envm、set、tinyblob、blob、mediumblob、logngblob、 tingtext短文本、text長文本、mediumtext中長度、longtext極大文本
5、數學函數:
abs(-3):絕對值 ceiling(x):>x的最小整數值
bin(x):返回二進制loct八進制,hex十六進制 exp(x):返回e(自然對數的底)的x次方
floor(x):<x的最大整數 greatest(,,,):最大值
least(,,,)最小值 ln(x):x的自然對數
log(x,y):x的以y爲底的對數 mod(x,y):x/y的模
pi():pi的值(圓周率) rand():隨機數生成器,生成一個指定的值
round(x,y):參數x的四捨五入的有y位小數的值 sign(x):返回代表數字x的符號的值,0 整數 負數(0,1,-1)
sqrt(x):平方根 truncate(x,y):數字x截短爲y位小數的結果
nvl(e1,e2):如果e1爲null,返回e2 nvl2(e1,e2,e3):如果e1爲null,則返回e3,不爲null返回e2
6、聚合函數:
avg():返回指定列的平均值 count():指定列中非空null值的個數
min():指定列最小值 sum():指定列所有值之和
group-concat()由屬於一組的列值連接組合而成的結果 decode():函數將查詢結果譯爲其他值
decode(column,值,譯值,缺省值):
7、字符串函數:
ascii(char):字符串的asc[]i碼值 bit_length(str):返回字符串的比特長度(位)
concat(,,,,):連城字符串,並用sep字符串間隔 instr():str從x處,y個字符長的子串替爲instr
find_in_set(str,list):分析逗號分隔的list表,返回str位置 lcase(str)/lower(str):str中所有字符改變爲小寫
left(str,x):str中最左邊的x個字符 length(str):str中的字符數
ltrim(str):str中切掉開頭的空格 quote(str):反斜槓轉譯str中單引號
repeat(str,srchstr,rplcstr):str重複x次的結果 reverse(str):顛倒字符串str的結果
right(str,x):字符串最右邊的x個字符 rtrim(str):str尾部的空格
strcmp(s1,s2):比較s1和s2 trim(str):去除str首尾的所有空格
ucase(str)/upper(str):str中轉爲大寫
8、日期和時間函數:
curdate()/current_date() :當前的日期
curtime()/current_time():當前時間
current_timestamp():時間戳
date_add(now(),interval(2 month)):當前時間加兩個月
date_sub(now(),interval(2 month)):當前時間減兩個月
dayofweek(now()):一週中的第幾天
dayofmonth(now()):一月中的幾天
dayofyear(now()):一年中的幾天
dayname(now()):星期名
hour(now()):小時
monthname(now()):月份名
quarter(now()):第幾個季度(1-4)
week(now()):第幾周(0-53)
9、格式化函數
date_format(now(),'%y/%m/%d %h/%i/%s')
10、系統信息函數
database():返回當前數據庫
version():返回mysql服務器版本
benchmark(2,expr):將表達式expr運行2次
connection_Id():返回當前客戶鏈接的id
found_rows():最後一個select查詢進行檢索的總行數
user()/system_user():當前登錄用戶名
11、算數運算符
+,-,*,/,%/mod
12、比較運算符
=,<>/!=,<=>安全的等,<,<=,>,>=,between.....and.....,in....,is not不是,is是,like '%孫%',not like
13、邏輯運算符
not/!,and/&&,or/||,xor異或,max(),min(),
14、自然連接
select class.classname,student.name from class,student where student.classid=class.id and student.classid=3;
15、自連接
select s.name from student s,student t where a.id=t.id and s.grade>90 and t.classid=3;
16、左外鏈接
select student.*,class.classname from student left outer join class on student.classid=class.id
17、右外連接
select student.*,class.classname from class right outer join student on class.id=student.classid
18、等值連接
select student.*,class.classname from student,class where student.classid=class.id
19、內鏈接
select student.*,class.classname from student inner join class on student.classid=class.id
20、五類約束
not null:非空約束
unique:唯一約束,某列或幾列組合的數據不能重複
primary key:主鍵約束,某列數據不能重複唯一
foreign key:外鍵
check:檢查,指定一個表達式,用於檢查指定數據(不起作用)
21、取消非空約束
alter table test modify id int null;
22、增加非空
alter table test modify id int not null default 1;
23、創建唯一約束
id int not null unique
constraint UK unique(id)
多列:constraint UK unique(id,password)組合不重複
刪除:alter table user drop index UK
增加:alter table user add constraint UK unique(id)
修改:alter table user modify id int unique
24、創建主鍵
id int primary key
constraint UK primary key(id)
刪除:alter table test drop primary key
添加:alter table test add constraint PK primary key(id)
修改:alter table test modify id int primary key(id)
25、主鍵自增
auto_increment
id int auto_increment primary key