數據庫簡介:
1.Oracle數據庫
- 提起數據庫,第一個想到的公司,一般都是Oracle(甲骨文)
2.SQL Server數據庫:微軟開發的,是web最流行的用於存儲數據的數據庫,廣泛應用於電子商務,銀行,保險,電力等與數據庫有關的行業
收費且不好用,因此,如今用的越來越少
3.MySQL數據庫(被Oracle收購)
4.操作命令:
- 啟動:mysql; (Linux中啟動:sudo service mysql start)
- 清屏:clear;
- 退出:exit;
-
登錄(進入)mysql常用的是:mysql -u root -p; (-u用戶名,-p用戶密碼,其中如果沒有設置用戶名則root為默認的用戶名)
- 敲擊后會提示輸入密碼(輸入后沒有顯示,隱碼輸入),如果剛安裝好MYSQL沒設置密碼,超級用戶root是沒有密碼的,故直接回車即可進入到MYSQL中
- 顯示當前所有數據庫:show databases;
- 進入(切換到)一個數據庫:use 數據庫名;
- 顯示當前數據庫內容(數據庫的所有表):show tables;
- 顯示某個表的所有數據:select * from 表名;
- 顯示表中的一些字段:select 字段1,字段2,字段3 from 表名; 例如:select id,name,salary from tbwork;
- 創建表:create table 表名
(id int,
name char(10),
age int,
score float);(各個屬性和對應類型設置好即可)
例1:
create table stu
(id int,
name char(10),
age int,
score float);
-
- 顯示表:show tables;
- 顯示表stu的結構:desc stu;(或者describe stu;)
例2:創建表class,id設置為主鍵,班級人數默認值為30(如果你不設置人數默認為30): PS:一旦id設置為主鍵,則在插入數據不能重復,且插入數據時必須加上id
create table class
(id int primary key, (若要不為空:id int primary key not null,)
name char(20),
num int default 30);
-
- 插入:insert into 表名(字段) values(字段) PS:多個加s
- 接着插入一條數據到表stu:insert into stu(id,name,age,score) values(101,'Tom',20,98);
PS:前面幾個字段,后面對應幾個字段,字符串用單引號或者雙引號;
2.插入一條數據到表class,不寫班級人數:
-
- 數據為:id是1004,name為Water
- 命令:insert into class(in,name) values(1004,'Water');
- 緊接着查詢class的信息:select * from class; (發現1004的人數為30,因為沒有插入人數,上面默認人數為30)
- 修改表stu:
- update 表名 set 修改字段和值 where 哪個id;
- 例如:修改表stu中id為101的分數為96:update stu set score=96 where id=101;
- 例如:表stu中每人年齡每過一年加1,怎么修改:update stu set age=age+1;
- 修改stu的結構,在stu中增加地址addr:alter table stu add addr char(60); (此時select * from stu可以發現表中score后面多了addr一項)
- 緊接着想在score和addr中間加生日bd:alter table stu add bd char(6) after score;
- 接着想改bd為出生年份year:alter table stu change bd year int;
- 刪除字段year:alter table stu drop year;
- update 表名 set 修改字段和值 where 哪個id;
- 查詢:
- 選擇查詢某一列:select 字段 from stu; 例如:select id from stu; (多列加上多個字段即可)
- 有條件選擇某一行:select * from stu where 條件;
- 例如:
- select * from stu where id=101;
- select * from stu where score>96;
- select * from stu where score<96 and score >94;
- select * from stu where score<96 and id=107;
- select * from stu where score>97 or score<93;
- select * from stu where name='Rose';
select * from stu where name like 'xLx';(like是通配符,xLx表示只要name中包含L的都選擇查詢出來)
select * from stu where name like 'Lx';(Lx表示name中L在開頭的)
select * from stu where name like 'xL';(Lx表示name中L在最后的的)
select * from stu where score like 'x9x';(分數score中有9的)
-
-
- select * from stu where id=101 or id=102 or id=103; 等價於:select * from stu where id in(101,102,103); (用in()集合更方便)
-
- 多表復合查詢:
- 例如:
- 在上面的表中,查詢表work中id等於表salary中工資salary為8500的wid的信息:select * from work where id=(select wid from salary where salary=8500);