mysql常見操作

1 mysql常用術(shù)語

? ? 1) 數(shù)據(jù)庫? database (***)

? ? 2) 表? table? (***)

? ? 3) 行(row)和列(column)

? ? 4) 記錄 (record) 一個數(shù)據(jù)行? 就是一條記錄

? ? 5)字段(field) 每個列 就是一個字段

? ? 備注: 結(jié)構(gòu)化查詢語言SQL(Structured query language)众羡,

? ? ? ? ? 操作關(guān)系數(shù)據(jù)庫的通用語言薇溃,用于執(zhí)行數(shù)據(jù)的檢索和其他操作铆惑。

? ? 數(shù)據(jù)庫 =》 表 =》 記錄(行)

2 mysql安裝

? ? 1) 下載 phpStudy 集成工具

? ? 2) 下一步 安裝 點擊啟動 (綠色代表成功)

? ? 啟動會同時啟動兩個東西:

? ? ? ? apache:? 這是集成的一個服務(wù)器? 端口是 80

? ? ? ? mysql:? 這是數(shù)據(jù)庫? 端口是 3306

3 連接數(shù)據(jù)庫

? ? 語法: mysql -h主機名? -u用戶名 -p密碼

? ? mysql –hlocalhost –uroot –proot

? ? 測試是否連接成功:

? ? ? ? show databases;

? ? 說明: 需要配置環(huán)境變量, 把以下路徑 放入環(huán)境變量

? ? ? ? C:\phpStudy\PHPTutorial\MySQL\bin

4 數(shù)據(jù)庫的操作 (**)

? ? 1) 查看數(shù)據(jù)庫列表

? ? ? ? show databases;

? ? 2) 創(chuàng)建數(shù)據(jù)庫

? ? ? ? create database 數(shù)據(jù)庫的名字;

? ? ? ? 例子: create database web;

? ? 3) 切換(使用數(shù)據(jù)庫)

? ? ? ? use 數(shù)據(jù)庫名稱;

? ? ? ? 例子: use web;

? ? 4) 刪除數(shù)據(jù)庫

? ? ? ? drop database 數(shù)據(jù)庫的名字;

? ? ? ? 例子: drop database web;

? ? 5) 修改數(shù)據(jù)庫的選項(一般修改編碼)

? ? ? ? alter database 數(shù)據(jù)庫的名字? 修改內(nèi)容

? ? ? ? 例子: alter database web charset=gbk;

? ? 補充: 查看創(chuàng)建數(shù)據(jù)庫的一些信息

? ? 6) show create database 數(shù)據(jù)庫的名字

? ? ? ? 例子: show create database web;

5. 表的基本操作(*****)

? ? 1) 創(chuàng)建表

? ? ? ? 語法:

? ? ? ? ? ? create table 表名 (

? ? ? ? ? ? ? ? 字段名稱 數(shù)據(jù)類型和長度 屬性修飾,

? ? ? ? ? ? 字段名2 字段類型2 屬性修飾2

? ? ? ? ? ? )[表選項]

? ? ? ? 例子:

? ? ? ? ? ? create table student (

? ? ? ? ? ? ? ? id int primary key auto_increment,

? ? ? ? ? ? ? ? name varchar(50),

? ? ? ? ? ? ? ? age int,

? ? ? ? ? ? ? ? sex varchar(10)

? ? ? ? ? ? );

? ? ? ? ? ? create table teacher (

? ? ? ? ? ? ? ? name varchar(50),

? ? ? ? ? ? ? ? age int,

? ? ? ? ? ? ? ? sex varchar(10)

? ? ? ? ? ? );

? ? 2) 查看當前數(shù)據(jù)庫有哪些表

? ? ? ? show tables;

? ? 3) 查看表的結(jié)構(gòu)

? ? ? ? desc 表的名字;

? ? ? ? 例子: desc student;

? ? 4) 刪除表

? ? ? ? drop table [if exists] 表名

? ? ? ? 例子: drop table if exists teacher;

? ? 補充:

? ? ? ? 5)修改表:

? ? ? ? ? ? alter table 表名 選項=新值

? ? ? ? ? ? 例子: alter table student charset=utf8;

? ? ? ? 6)查看建表的一些信息:

? ? ? ? ? ? show create table 表的名字

? ? ? ? ? ? 例子: show create table student;

6 記錄的基本操作 (******)

? ? 1) 增加記錄(插入數(shù)據(jù)) ------ 增

? ? ? ? 語法: insert into 表名 (字段列表) values (值列表)

? ? ? ? 例子: insert into teacher(name, age, sex) values('李尋歡', 35, '男');

? ? ? ? ? ? ? insert into teacher(name, age, sex) values('xunhuanLi', 35, 'man');

? ? ? ? ? ? ? insert into student(name, age, sex) values('小張', 20, '男');

? ? ? ? ? ? ? insert into student(name, age, sex) values('小李', 21, '女');

? ? ? ? ? ? ? insert into student(name, age, sex) values('小王', 22, '男');

? ? ? ? 補充:?

? ? ? ? ? ? ? insert into 表明 values (值列表)? // 參數(shù)一定要按照順序

? ? ? ? ? ? ? 例子:? insert into teacher values('老王', 35, '男');? ?

? ? ? ? ? ? ? ? ? ? insert into student values(default,'小強', 18, '男');?


? ? ? ? ? ? ? 注意: 如果使用這種 省略字段列表的方式 需要使用 default 或 null 占位

? ? ? ? dos控制臺中文編碼問題 (*****)

? ? ? ? ? ? 在插入數(shù)據(jù)之前 set names gbk;


? ? 2) 查詢數(shù)據(jù) (*******) ------- 查

? ? ? ? a) 基本查詢:

? ? ? ? ? ? 語法: select 字段列表 from 表的名字渔呵;

? ? ? ? ? ? 例子: select * from student;? // 查詢表里面的所有字段

? ? ? ? ? ? ? ? ? select name from student;? // 查詢表里面的name字段?

? ? ? ? ? ? ? ? ? select age from student;? // 查詢表里面的age字段?

? ? ? ? ? ? ? ? ? ? 補充: 查詢出來 取別名

? ? ? ? ? ? ? ? ? ? select 字段名 as 別名 from 表名;

? ? ? ? ? ? ? ? ? ? 例子: select name as n from student;

? ? ? ? b) 帶條件查詢

? ? ? ? ? ? 語法:? select 字段列表 from 表的名字 where 條件

? ? ? ? ? ? 例子: select * from student where sex = '女';

? ? ? ? ? ? ? ? ? select * from student where sex <> '女';

? ? ? ? ? ? ? ? ? select * from student where age > 18;

? ? ? ? ? ? ? ? ? select * from student where age < 21;

? ? ? ? ? ? ? ? ? select * from student where age >= 18;

? ? ? ? ? ? ? ? ? select * from student where age <= 21;

? ? ? ? ? ? ? ? ? select * from student where age BETWEEN 20 AND 22;? // 在什么之間

? ? ? ? ? ? ? ? ? select * from student where age > 18 and sex='女'; // 并且

? ? ? ? ? ? ? ? ? select * from student where age > 18 or id=6;? // 或者

? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '小%';? // 正則匹配以。塌西。開頭的

? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '老%';

? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '%強';? // 正則匹配以柔吼。。結(jié)尾的

? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '%李';

? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '%李%';? // 包含這個字符的

? ? ? ? ? ? ? ? ? ? select * from student where name LIKE '%王%';

? ? 3) 刪除數(shù)據(jù) (****)------? 刪

? ? ? ? 語法: delete from 表名 [where 條件]

? ? ? ? 例子: delete from student where sex = '女';? // 刪除所有性別為女的數(shù)據(jù)

? ? ? ? 例子: delete from student where id = '8';? // 刪除id = 8數(shù)據(jù)

? ? ? ? 備注: 如果不寫條件 代表刪除所有數(shù)據(jù)

? ? ? ? ? ? 例如: delete from student;? // 刪除 student 表 所有數(shù)據(jù)

? ? 4) 修改數(shù)據(jù) (****) -----改

? ? ? ? 語法: update 表名 set 字段=值, 字段=值 [where 條件表達式];

? ? ? ? 例子: update student set age = 38, sex = '女';

? ? ? ? ? ? ? update student set age = 8, sex = '男' where id = 1;

7 數(shù)據(jù)類型

? ? 商品表 (關(guān)鍵字 可以使用反引號)

? ? create table `goods` (

? ? ? ? id int primary key auto_increment,

? ? ? ? `sort` tinyint unsigned,

? ? ? ? name varchar(100),

? ? ? ? price float(10, 2),

? ? ? ? `desc` text,

? ? ? ? ctime TIMESTAMP DEFAULT CURRENT_TIMESTAMP

? ? );

? ? insert into goods(name, price, `desc`) values('ipone18', 9999.999, '這是一個手機1');

? ? insert into goods(name, price, `desc`) values('ipone19', 8888.999, '這是一個手機2');

? ? insert into goods(name, price, `desc`) values('ipone20', 7777.999, '這是一個手機3');

? ? 補充: 查詢結(jié)果排序

? ? select * from goods order by ctime asc;? // 升序 默認

? ? select * from goods order by ctime desc; // 降序排列

? ? select * from goods where 1 order by ctime desc;? // 條件可以拼接 order by 只能在最后

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末牺陶,一起剝皮案震驚了整個濱河市伟阔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌掰伸,老刑警劉巖皱炉,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異狮鸭,居然都是意外死亡合搅,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門歧蕉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來灾部,“玉大人,你說我怎么就攤上這事惯退《乃瑁” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵催跪,是天一觀的道長锁蠕。 經(jīng)常有香客問我,道長懊蒸,這世上最難降的妖魔是什么荣倾? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮骑丸,結(jié)果婚禮上逃呼,老公的妹妹穿的比我還像新娘。我一直安慰自己者娱,他們只是感情好抡笼,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著黄鳍,像睡著了一般推姻。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上框沟,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天藏古,我揣著相機與錄音增炭,去河邊找鬼。 笑死拧晕,一個胖子當著我的面吹牛隙姿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播厂捞,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼输玷,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了靡馁?” 一聲冷哼從身側(cè)響起欲鹏,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎臭墨,沒想到半個月后赔嚎,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡胧弛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年尤误,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片结缚。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡袄膏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出掺冠,到底是詐尸還是另有隱情,我是刑警寧澤码党,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布德崭,位于F島的核電站,受9級特大地震影響揖盘,放射性物質(zhì)發(fā)生泄漏眉厨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一兽狭、第九天 我趴在偏房一處隱蔽的房頂上張望憾股。 院中可真熱鬧,春花似錦箕慧、人聲如沸服球。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽斩熊。三九已至,卻和暖如春伐庭,著一層夾襖步出監(jiān)牢的瞬間粉渠,已是汗流浹背分冈。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留霸株,地道東北人雕沉。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像去件,于是被迫代替她去往敵國和親坡椒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容