--數(shù)據(jù)庫操作
--鏈接數(shù)據(jù)庫
mysql -uroot -p
--退出數(shù)據(jù)庫
exit
--查詢數(shù)據(jù)庫版本
select version();
--查看所有數(shù)據(jù)庫
show databases;
--創(chuàng)建數(shù)據(jù)庫
create database name charset=utf8;
--刪除數(shù)據(jù)庫
drop database name;
--切換至指定數(shù)據(jù)庫
use name;
--查看當(dāng)前使用的數(shù)據(jù)庫
select database();
--表操作
--創(chuàng)建表
--約束條件:
--auto_increment 自動增長
--not null 不能為空
--primary key 主鍵
--default 默認(rèn)值
create table staff (
--字段 類型 約束
id int unsigned primary key not null auto_increment,
staff varchar(20) not null,
age tinyint unsigned default 0,
sex enum('男','女','未知') default '未知',
createTime timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,
)
--刪除表
drop table staff;
--查看當(dāng)前數(shù)據(jù)庫所有的表
show tables;
--查看指定表結(jié)構(gòu)
desc staff;
--字段的增刪改
--新增字段名 add
alter table staff add position varchar(20) default "員工";
--刪除字段名 drop
alter table staff drop position;
--修改字段(不修改名稱) modify
alter table staff modify position varchar(25) default "圖書管理員";
--修改字段(包含名稱) change
alter table staff change position post varchar(25) default "圖書管理員";
--數(shù)據(jù)操作
--添加表數(shù)據(jù)
insert into staff values(0,'水君子',25,'男','員工');
--查看表數(shù)據(jù)
select * from staff;
--添加多行數(shù)據(jù)
insert into staff values(0,'鄭治',19,'男','員工'),(0,'李清',22,'女','員工');
--修改全部
update staff set position="員工";
--修改指定
--把id為1的name改為水君子
update staff set name="水君子",age=24 where id=1;
--物理刪除
--刪除整個staff表
delete from staff;
--刪除單個
delete from staff where name="水君子";
--刪除多個
delete from staff where name="水君子" or name="張三";
--邏輯刪除
--用一個字段來表示狞膘,這條信息已無法使用
--bit類型
alter table staff add is_delete bit default 0;
update staff set is_delete=1 where name="水君子";
--查詢
--查詢指定字段
select id,name from staff;
--消除重復(fù)
select distinct sex from staff;
--條件查詢
--> < = !=
select * from staff where age >18;
--邏輯運算符
--and or not
select * from staff where sex="男" and age>18;
select * from staff where age>18 or sex="男";
select * from staff where not(age >18 and sex="男");
--模糊查詢
--like 性能差
--鄭開頭的名稱
select * from staff where name like "鄭%";
-- 包含333的手機(jī)號
select * from staff where phone like "%333%";
--匹配名字兩個的
select * from staff where name like "__";
--匹配至少兩個的
select name from staff where name like "__%";
--rlike 正則
-- 鄭開頭的名字
select * from staff where name rlike '^鄭.*';
-- 13開頭6結(jié)尾
select * from staff where phone rlike '^13.*6$';
--范圍查詢
--非連續(xù) in() 揩懒、 not in ()
select * from staff where age in (18,30);
select * from staff where age not in (18,30);
--連續(xù) between
select * from staff where age between 18 and 30;
select * from staff where age not between 18 and 30;
select * from staff where (age between 18 and 25);
--空判斷
select * from staff where sex is null;
select * from staff where sex is not null;
--排序
--order by 字段
--asc升序
--desc降序
select * from staff order by age asc;
select * from staff order by age desc;
select * from staff where (age between 18 and 30) order by age asc;
--同一歲數(shù)的性別升序
select * from staff where (age between 18 and 30) order by age asc,sex desc;
常用mysql語句
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門弃舒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人状原,你說我怎么就攤上這事聋呢。” “怎么了颠区?”我有些...
- 文/不壞的土叔 我叫張陵削锰,是天一觀的道長。 經(jīng)常有香客問我毕莱,道長器贩,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任央串,我火速辦了婚禮磨澡,結(jié)果婚禮上碗啄,老公的妹妹穿的比我還像新娘质和。我一直安慰自己,他們只是感情好稚字,可當(dāng)我...
- 文/花漫 我一把揭開白布饲宿。 她就那樣靜靜地躺著,像睡著了一般胆描。 火紅的嫁衣襯著肌膚如雪瘫想。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼窄驹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了证逻?” 一聲冷哼從身側(cè)響起乐埠,我...
- 正文 年R本政府宣布,位于F島的核電站键菱,受9級特大地震影響谬墙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜经备,卻給世界環(huán)境...
- 文/蒙蒙 一拭抬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧侵蒙,春花似錦造虎、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至犁功,卻和暖如春氓轰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背浸卦。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 推薦:http://www.cnblogs.com/picaso/archive/2012/05/17/25058...
- 一蛤奥、登錄數(shù)據(jù)庫 命令行登錄使用的主要參數(shù): 不用輸入密碼登錄數(shù)據(jù)庫?在用戶家目錄新建 .my.cnf文件(修改權(quán)限...
- 國際上有個日子凡桥,不知大家是否聽說過蟀伸,國際不再恐同日 日期為5月17日(源于1990年5月17日世界衛(wèi)生組織 (WH...