一账千、window 系統(tǒng)終端命令基礎(chǔ)安裝
win+r 打開(kāi)終端 :cmd中進(jìn)行
安裝: mysqld -install
啟動(dòng): net start mysql
鏈接登錄:
1)mysql -uroot -proot (用戶(hù)名:root捌年, 密碼:root)
或者
2)mysql -hlocalhost -p3306 -uroot –proot
(-h代表主機(jī)地址 -p3306—MySQL默認(rèn)端口未被占用的前提下)
或者
3)mysql -hlocalhost -p3306 -uroot -p "回車(chē)"
Enter password:
退出: exit或者quit;
二缩多、數(shù)據(jù)庫(kù)管理系統(tǒng)(DBMS)分類(lèi)
1.基于共享文件系統(tǒng)的DBMS吏廉,如Microsoft Access和FileMaker啰脚。
2.基于客戶(hù)機(jī)—服務(wù)器的DBMS询张,如MySQL(默認(rèn)端口號(hào):3306)谱轨、 Oracle(默認(rèn)端口號(hào):1521)以及Microsoft SQL Server(默認(rèn)端口號(hào):1433);
三戒幔、SQL(Structured Query Language)
1】SQL是一種專(zhuān)門(mén)用來(lái)與數(shù)據(jù)庫(kù)通信的結(jié)構(gòu)化查詢(xún)語(yǔ)言。
2】數(shù)據(jù)庫(kù)(database)保存有組織的數(shù)據(jù)的容器(通常是一個(gè)文件或一組文件)土童。
3】表(table) 某種特定類(lèi)型數(shù)據(jù)的結(jié)構(gòu)化清單诗茎。
4】列(column) 表中的一個(gè)字段。所有表都是由一個(gè)或多個(gè)列組成的献汗。
5】數(shù)據(jù)類(lèi)型(datatype) 所容許的數(shù)據(jù)的類(lèi)型敢订。每個(gè)表列都有相應(yīng)的數(shù)據(jù)類(lèi)型,它限制(或容許)該列中存儲(chǔ)的數(shù)據(jù)罢吃。
6】行(row) 表中的一個(gè)記錄楚午。
四、MySQL常用語(yǔ)法
【1】數(shù)據(jù)“庫(kù)”語(yǔ)法
1.顯示所有的數(shù)據(jù)庫(kù):show databases;
2.創(chuàng)建數(shù)據(jù)庫(kù):
1)create database databasename;
或者
2)create database [if not exists] databasename;
例如:create database students;
create database if not exists students1;
3.刪除數(shù)據(jù)庫(kù): drop database databasename;
例如:drop database students;
4.導(dǎo)入sql文件: source 被導(dǎo)文件所在的路徑/被導(dǎo)文件名
(若是錯(cuò)誤則改成\)
source E:/MySQL/testdata/mysql_scripts/create.sql
source E:/MySQL/testdata/mysql_scripts/populate.sql
5.查詢(xún)版本號(hào):select version();
6.查看報(bào)錯(cuò)信息: show errors;
7.選中數(shù)據(jù)庫(kù):use databasename;(分號(hào)可省略)
例如:use students1
【2】“表格”語(yǔ)法
1.顯示所有可能的表:show tables;
2.顯示表結(jié)構(gòu):
1)desc tablename;
2)show columns from tablename;
3)show full columns from tablename; 相對(duì)于上2條更詳細(xì)(備注等信息)
例如:desc students1;
show columns from students1;
show full columns from students1;
【3】簡(jiǎn)單數(shù)據(jù)查詢(xún)語(yǔ)法:
1.查詢(xún)所有列: select * from tablename;
例如:select * from students1;
2.查詢(xún)多列: select columnname1,columnname2,…,columnnameN from tablename;
例如:select id,name,age from students1;
3.查詢(xún)單列: select columnname from tablename;
例如:select id from students1尿招;
4.去重復(fù)醒叁,查詢(xún)不同的數(shù)據(jù):distinct
例如:
select distinct prod_price from products;
select distinct prod_price,prod_desc from products;
5.排序:order by 默認(rèn)升序(asc),降序(desc)
例如:
select prod_price from products order by prod_price;
select prod_price from products order by prod_price asc;
select prod_price from products order by prod_price desc;
select prod_price,prod_id from products order by prod_price desc ,prod_id; 表示先進(jìn)行價(jià)格降序排列,再進(jìn)行編號(hào)升序排序
6.限制查詢(xún):limit
limit n:顯示前n條記錄
limit m,n:顯示從行m(第m+1行)開(kāi)始的n行;
例如:
select prod_price from products limit 5;
select prod_price from products limit 5,3;
limit 和order by聯(lián)用可以顯示最高或者最低的幾個(gè);
例如:
select prod_price from products order by prod_price desc limit 3;
select prod_price from products order by prod_price desc limit 1;
select ceshi81.products.prod_price from ceshi81.products order by prod_price desc limit 1;
【4】過(guò)濾數(shù)據(jù)查詢(xún)法:where
- = , > , < ,>= , <=, != or <> 不等于
例如:
select prod_id from products where vend_id=1001;
select * from products where prod_price>10;
select * from products where prod_name='apple';
select * from products where prod_name>'Fuses'; 字符從第一個(gè)字符開(kāi)始比較泊业,比F大把沼;若相同則進(jìn)行第二個(gè)字符比較比u大
select distinct prod_name from products where vend_id=1003 limit 3;
2.區(qū)間: between and 含邊界
Not between and
例如:select prod_price from products where prod_price between 10 and 35; 10<=prod_price<=35
select prod_price from products where prod_price not between 10 and 35;
3.空值查詢(xún): is null
is not null
例如: select * from vendors where vend_state is NULL;
select * from vendors where vend_state is not NULL;
【5】組合where子句查詢(xún)法:
1.and 同時(shí)滿足
select * from products where vend_id=1003 and prod_price <=10;
2.or 滿足其中一個(gè)條件
select * from products where vend_id=1003 or vend_id=1005;
select * from products where vend_id=1003 or prod_price <=10 ;
select * from products where vend_id=1003 or vend_id=1005 and prod_price<=10.0; and優(yōu)先級(jí)比or高
select * from products where vend_id=1003 or (vend_id=1005 and prod_price<=10.0); 先進(jìn)行括號(hào)里面的篩選
3.in 操作符(等同于or) [not in]
select * from products where vend_id=1003 or vend_id=1005 or vend_id=1001;
select * from products where vend_id in(1001,1003,1005);
綜合案例:
select distinct prod_price
from products
where vend_id in (1001,1003,1005)
order by prod_price desc
limit 3;