1. 程式人生 > >資料庫的基本操作增刪查改

資料庫的基本操作增刪查改

1、啟動資料庫

service mysqld start

2、連線資料庫

mysql -u root -p

3、建立資料庫

create database 庫名;

4、使用資料庫

use database;

5、檢視資料庫

show databases;

6、刪除資料庫

drop database 資料庫名;

7、增加

insert into table_name[(column[,column...])]    values (value [,value...]); 
例子:
mysql> insert into goods values(101, '披薩', 27.5);  
插入的資料應與欄位的資料型別相同。比如,將‘abc'插入到id列就不行:

當主鍵存在衝突的時候(duplicate key),可以選擇性的進行處理
7.1更新操作

insert into 表名(欄位列表) values(值列表) on duplicate key update 欄位=新值;  
例子:
 insert into goods values(101, 'ccc', 20.5) on duplicate key update goods_name='ccc',  price=20.5;  

7.22.替換:主鍵如果沒有衝突,就直接插入

replace into 表名(包含欄位) values(值列表); 
例子
mysql> replace into goods values(100, 'huawei', 999);  Query OK, 2 rows affected (0.01 sec) 

8、修改
語法

update tbl_name set col_name1=expr1, [, col_name2=expr2 ...] [where conditon] 
例子:
1.mysql> update goods set price=300; ‐‐ 沒有條件,整表修改     //將所有產品的價格修改為300元
2、update goods set price=1000 where id=100;       //將id為100的產品價格修改為1000
3、update goods set price=price+200 where id=101;  //將id為101的產品價格增加200 

update使用細節:
update 語法可以用心值更新原有表中的各列
set子句指示要修改哪些列和要給予哪些值
where子句指定應更新哪些行。如果沒有where子句,則更新所有行
如果需要更新多個欄位,可以通過 set 欄位1=值1,欄位2=值2…
9、刪除
語法:

1、delete from  tbl_name [where condition] 
例子:
mysql> delete from goods where id=101;  //刪除表中id為101的資料
mysql> create table goods2 like goods;  //複製表的結構
2、刪除表中所有記錄
mysql> delete from goods; ‐‐刪除整個表的資料,但是表的結構還存在 
3、mysql> truncate table goods; ‐‐這個指令也把整個表記錄刪除 

上述兩種刪除整表的區別:
效果一樣,truncate速度快,delete可以帶where條件,刪除更加靈活
delete可以返回被刪除的記錄數,而truncate返回0,推薦使用delete
delete使用細節: 如果不適用where子句,將刪除整個表中所有資料 delete語句不能刪除某一列的值(可以用update置null) 使用delete語句僅刪除記錄,不刪除表本(drop table)
10、查詢selsct

1、可以指定查詢哪些列,比如:查詢id,姓名,數學成績
mysql> select id, name, math from student;  
2、*號表示查詢所有列( 星號效率很低,用哪些欄位就取哪些欄位 )
mysql> select * from student; 
3、distinct 如果結果中有完全相同的行,就去除重複行
mysql> select distinct math from student;  
4、select column as 別名 from 表; //a表示起別名
mysql> select name, chinese+math+english+10 as total from student;
5.將所有行唐的學生成績增加60%(查詢總分再增加60%)
mysql> select name, (chinese+math+english)*1.6 as total from student where name like '唐%'; ‐‐ 增 加60%就是 乘以 1.6, like 是模糊查詢 



**select的where子句,使用where進行查詢過濾**
mysql> select * from student where name like '李%';  //查詢姓李學生的成績
mysql> select * from student where english > 90; //查詢英語成績大於90的同學
mysql> select id, name, math+english+chinese as 'total' from student where math+english+chinese  >200;  //查詢總分大於200的所有同學
mysql> select * from student where name like '李%' and id > 10;  //查詢姓李並且成績大於100的學生
mysql> select * from student where english > chinese;  //查詢英語成績大於語文成績的學生
mysql> select * from student where (math+english+chinese) > 200 and math < chinese and name like  '唐%';    //查詢總分大於200分並且數學成績小於語文成績的姓唐的學生
mysql> select * from student where english>=80 and english<=90; ‐‐方法1  mysql> select * from student where english between 80 and 90; ‐‐ 方法2 between是閉區間 
//以上兩種方法表示查詢英語分數在80-90之間的同學
mysql>  select * from student where math in(89,90,91);//查詢數學成績為89,90,91的同學


**select的order by字句**
select column1,column2,... from table order by column  asc|desc,...; 
order by 指定排序的列,排序的列可以使表中的列名,也可以是select語句後指定的別名
 asc升序(預設),desc降序 
 order by 子句應該位於select語句的結尾
mysql> select * from student order by math;‐‐沒有指定升序還是降序,預設是升序 
mysql> select id, name, math+english+chinese as total from student order by total desc; //總分排序後,從高到低輸出
mysql> select id, name, math+english+chinese as total from student where name like '李%' order by  total;  //對姓李的同學按成績從低到高排序


**count:返回某一列、行的總數**
select count(*)|count(列名) from tbl_name where condition 
mysql> select count(*) from student; //統計一個班總共有多少個學生
mysql> select count(*) from student where math>=90; //統計數學成績大於90的學生人數
mysql> select count(*) from student where math+english+chinese>250;  //統計總成績大於250的學生人數


**sum:sum函式返回滿足where條件的行的和**
mysql> select sum(math) from student;//統計一個班級數學總成績
mysql> select sum(chinese), sum(english), sum(math) from student;  //統計一個班語文英語數學各科的總成績
mysql> select sum(chinese+english+math) from student;  //統計一個班語文英語數學成績總和
mysql> select sum(chinese)/count(name) from student;  //統計一個班語文成績平均分



**avg:agv函式返回滿足where條件的一列的平均值**
mysql> select avg(math) from student;  //求一個班數學平均分
mysql> select avg(math+chinese+english) from student;  //求一個班總平均分



**max/min函式返回滿足where條件的一列的大/小值**
mysql> select max(chinese+english+math), min(chinese+english+math) from student;  //求一個班的最高分和最低分




**group by 子句對列進行分組**
select column1, column2, .. from table group by column;
為了講清楚分組,建立一個僱員資訊表(來自oracle 9i的經典測試表)
1. EMP員工表 2. DEPT部門表 3. SALGRADE工資等級表
mysql> select deptno,avg(sal),max(sal) from EMP group by deptno;  //顯示各部門的平均工資個最高工資
mysql> select avg(sal),min(sal),job, deptno from EMP group by deptno, job;  //顯示每個部門的每種崗位的平均工資和低工資