sudo /etc/init.d/mysql start(開機) | restart(重啓) |stop(關機) |status(狀態)
mysql -hlocalhost -uroot -p123456() 連接服務器(-h主機地址 -u用戶名 -p密碼(可直接跟後面))
庫
查看已有庫:show databases;
創建庫:create database 庫名;
創建庫的同時指定字符集:create database 庫名 character set utf8;
查看創建庫的字符集:show create databases 庫名;
查看當前所在庫:select database();
切換庫:use 庫名;
查看當前庫中所有的表:show tables;
刪除庫:drop database 庫名;
表
創建表:create table 表名(字段名 數據類型,字段2 數據類型...);
創建表指明字符集:create table表明(字段1 數據類型,
字段2 數據類型,....)character set utf8;
查看已有表的字符集:show crate table 表名 ;
查看錶結構:desc 表名;
刪除表:drop table 表名;
表插入:
1.insert into 表名 values(值1),(值2),.....;
例:insert into stuinfo values(1,‘張三丰’,300),(值2),....;
2.insert into 表名(字段1,字段2,....)values(值1),(值2),...;
例:insert into stuinfo(name,age)values(‘小昭’,20),(‘敏敏’,20);
表查詢:(select)
1.select*from 表名;查詢表中的所有數據
2.2.select *from 表名 where 條件;按條件查找
3.select 字段1,字段2 from 表名 where 條件;
表字段(添加):
alter table 表名 執行動作
1.alter table 表名 add 字段名 數據類型;在末尾添加
2.alter table 表名 add 字段名數據類型 first;在第一列添加
3.alter table 表名 add 字段名 數據類型 after 已有字段名;指定位置後面添加
表字段(刪除):
alter table 表名 drop 字段名;
表字段(修改):
alter table 表名 change 舊字段名 新字段名 類型(寬度);
表 數據類型修改:
alter table 表名 modify 字段名 新的數據類型
表 重命名:
alter table 表名 rename 新表名;
表記錄(刪除):
delete from 表名 where 條件;
where 條件省略表示清空表記錄
表更新(update):
1.update 表名 set 字段 1=值,字段2=值,....where 條件;
2.update 必須寫where 條件
數據類型
**整型:**
1.int大整型(4個字節)取值範圍:0-2(32)-1
2.tinyint微小整型(1個字節)
1.有符號整型(默認),取值範圍-128~127
id tinyint signed
2.無符號整型(unsigned),取值範圍0~255
age tinyint unsigned
3.smallint 小整型(2字節)
4.bigint 極大整型(8字節)
**浮點型:**
1.float(4個字節,最多顯示7個有效位)
字段名 float(m,n);m表示總位數,n表示小數位的位數
2.double(8個字節)
3.decimal(最多可顯示28個有效位)
decimal(m,n);m表示總位數,n表示小數位的位數
**字符類型:**
1.char(m);取值範圍1~255
char(10)
2.varchar(m);取值範圍:1~65535
varchar(10)
3.text /longtext(4G)/blob/longblob(4G)
**枚舉:**
enum 最多可以列舉65535個值
例:create table userinfo(id int,name char(10),sex enum(‘男’,‘女’,‘保密’));
select * from userinfo where sex=1;
特點:
枚舉中的數據,從左到右會自動分配索引,從1開始,查詢數據時,可以根據字符串值進行查詢也可以根據索引值查詢
**集合:**
set
create table cousetab(sportcourse set(‘籃球’,‘足球’,‘球球’,...));
insert into cousetab values('籃球','足球','去球');
**日期時間:**
1.date:“YYY-MM-DD”
2.time:“hh:mm:ss”
3.datetime:“YYYY-MM-DD hh:mm:ss”;不給值,默認返回null
4.timestamp:“YYYY-MM-DD hh:mm:ss”;timestamp:不給值,默認返回當前 時間
日期時間函數
1.now()返回當前系統時間
2.curdate()返回當前日期(年月日)
3.Curtime()返回當前時間(時分秒)
4.year(date)返回指定日起的年份
5.date(‘20111212121212’)返回指定日期的年月日
6.time(date)返回指定日期的時分秒
例:select id,name,cztime from info2 where date(cztime)='20180830';
日期運算格式
select *from 表名 where 字段名 運算符(時間-interval 時間單位)
時間單位:1day |2 hour| 1 minute| 1 year |1 month 正值表示一天前
cztime>(now()-interval 1 day);
運算符
1.數值比較/字符比較
1.數值比較:= != > >= < <=
2.字符比較: = !=
2.邏輯運算符
1.條件1 and 條件2 and 條件3 and....;(查詢同時滿足多個條件的數據)
2.條件1 or 條件2 ;查詢滿足條件1或者滿足條件2的數據
3.範圍內比較
1.between..值,...and....值
設置範圍在 值1 和值2 之間
2.where 字段名 in(值1,值2,值3,...)
匹配字段在in給出的範圍內的數據
et:
where age in(22,23,24,25);
3.where 字段名not in(值1,值2,...);
匹配字段值不在指定範圍內的數據
匹配空,非空
1.匹配空: where 字段 is null;
2.匹配非空:where 字段 is not null;
模糊查詢
1.格式:where 字段名 like 表達式
2.表達式
1._:表示匹配1個字符
2.%:表示匹配0到多個字符
(2個字符及以上)mysql> select *from sanguo where name like '_%_';
(0個字符及以上)mysql> select*from sanguo where name like '%';
注意:
null不會被匹配出來
空字符串表示0個字符,會被正常匹配
3.匹配姓名爲3個字符串的數據
where name like '___';
4.匹配姓趙的數據
where name like '趙%_';
排序(order by)
order by:對查詢結果進行排序
1.格式:..order by 字段名 asc(升序)/desc(降序)
sql> select * from sanguo order by fangyu desc
分頁查詢(limit)
永遠放在sql語句的最後書寫
分頁用來控制顯示多條結果中的數據
1.語法:
1.limit n;表示顯示n條數據
2.limit m,n;
表示從第m+1條記錄開始顯示,顯示n條
select * from sanguo where country='蜀國' and name is not null order by gongji desc limit 0,3;
聚合函數—查詢
select *
select 字段
select 聚合函數 where....
1.聚合函數
最大值 最小值 求和 平均值 計數
max() min() sum() avg() count()