Day01 -資料庫和表基本操作1
劉國斌 QQ: 77331283
-
資料庫: 學習如何對資料進行增刪改查 4
-
Web前端: 學習如何開發頁面 7
-
SpringBoot: 學習如何解析請求 以及如何做出響應 9
資料庫
-
學習資料庫就是學習如何對資料進行增刪改查操作
-
DBMS: DataBase Management System 資料庫管理系統(資料庫軟體)
-
常見的DBMS:
-
MySQL: Oracle公司產品, 09年MySQL被Sun公司收購, 10年Sun公司被Oracle收購, MariaDB(原MySQL團隊公司開發的) , 市佔率排名第一 , 開源
-
Oracle: Oracle公司產品, 閉源,價格最貴,效能最強 , 市佔率排名第二
-
SQLServer: 微軟公司產品, 閉源 , 市佔率排名第三
-
DB2: IBM公司產品
-
SQLite: 輕量級資料庫,主要應用在移動裝置或嵌入式裝置中
-
SQL語言(Structured Query Language)
-
Structured Query Language結構化查詢語言
-
作用: 負責程式設計師和資料庫軟體進行交流
-
-
舉例:
insert into user values('tom','123456');
select username,password from user;
delete from user where name='tom';
update user set name='jerry' where name='tom';
電腦上安裝資料庫軟體(MySQL或MariaDB)
-
安裝步驟參見蒼老師文件伺服器
安裝教程:Windows MySQL安裝配置 http://doc.canglaoshi.org/doc/mysql.html
注意:如果之前已經安裝了MySQL或者MariaDB其中一個,此時安裝資料庫名和埠號要修改,port可以由3306設定為3307。
下載Git工程 裡面有筆記
https://gitee.com/teduliu/mysql05.git
連結資料庫
-
確保資料庫中已經安裝了資料庫軟體
-
找到MySQL Client 開啟
-
退出連結: exit;
-
連結指令(登入): mysql -uroot -p
-
如果是Linux或mac os 開啟終端 輸入上面的內容: mysql -uroot -p
資料庫和表的概念
-
如果需要在MySQL裡面儲存資料, 需要先建庫,再建表, MySQL裡面可以有n個數據庫, 一個數據庫裡面可以有n張表, 一張表裡面可以有n條資料。
-
即:資料庫---表---資料
資料庫格式:
-
以分號結尾
-
不區分大小寫
-
可以包含空格和換行
和資料庫相關的SQL語句
-
查詢所有資料庫
-
格式: show databases;
-
建立資料庫
-
格式: create database 資料庫名 charset=utf8/gbk; 或 create database 資料庫名 character set utf8;
-
舉例:
create database db1;
create database db2 charset=utf8;
create database db3 charset=gbk;
show databases;
-
查詢資料庫資訊
-
格式: show create database 資料庫名;
-
舉例:show create database db1;
-
刪除資料庫
-
格式: drop database 資料庫名;
-
舉例:
drop database db3;
drop database db2;
-
使用資料庫
-
在進行表相關或資料相關的操作之前必須使用了某一個數據庫
-
格式:use 資料庫名;
-
舉例:use db1;
練習題:
-
建立mydb1和mydb2資料庫 字符集第一個是utf8,第二個是gbk
create database mydb1 charset=utf8;
create database mydb2 charset=gbk;
-
查詢所有資料庫檢查是否建立成功
show databases;
-
分別查詢mydb1和mydb2 檢查字符集是否正確
show create database mydb1;
show create database mydb2;
-
先使用mydb1,再使用mydb2;
use mydb1;
use mydb2;
-
刪除兩個資料庫
drop database mydb1;
drop database mydb2;
表相關的SQL語句
-
建立表
-
格式: create table 表名(欄位1名 型別,欄位2名 型別)charset=utf8或gbk;
-
舉例:
create table person(name varchar(50));
create table student(name varchar(50),age int)charset=utf8;
create table hero(name varchar(50),type varchar(30),price int)charset=gbk;
-
-
查詢所有表
-
格式: show tables;
-
-
查詢表資訊
-
格式: show create table 表名;
-
舉例:show create table hero;
-
-
查詢表字段
-
格式: desc 表名;
-
舉例:desc hero;
-
-
刪除表
-
格式: drop table 表名;
-
舉例:
drop table hero;
drop table student;
-
練習題:
-
建立mydb3資料庫,指定字符集utf8,並使用
create database mydb3 charset=utf8;
use mydb3;
-
建立hero英雄表,設定名字和價格欄位、字符集utf8
create table hero(name varchar(50),price int)charset=utf8;
-
查詢所有表檢查是否建立成功
show tables;
-
查詢hero表字符集
show create table hero;
-
查看錶欄位
desc hero;
-
刪除hero表
drop table hero;
-
刪除資料庫mydb3
drop database mydb3;
表相關SQL(續)
-
修改表名
-
格式: rename table 原名 to 新名;
-
舉例:rename table hero to t_hero;
-
-
新增表字段
-
最後新增格式: alter table 表名 add 欄位名 型別;
-
最前面新增格式: alter table 表名 add 欄位名 型別 first;
-
在xxx欄位後面新增: alter table 表名 add 欄位名 型別 after xxx;
-
舉例:
create table emp(name varchar(50));
alter table emp add age int;
alter table emp add id int first;
alter table emp add salary int after name;
-
-
刪除表字段
-
格式: alter table 表名 drop 欄位名;
-
舉例:alter table emp drop salary;
-
-
修改表字段
-
格式: alter table 表名 change 原名 新名 新型別;
-
舉例:
alter table emp change age salary int;
alter table emp change salary job varchar(10);
-
表相關SQL回顧:
-
建立: create table t1(name varchar(20),age int)charset=utf8/gbk;
-
查詢所有: show tables;
-
查詢表資訊: show create table t1;
-
表字段; desc t1;
-
刪除表 drop table t1;
-
修改表名: rename table t1 to t2;
-
新增表字段; alter table t1 add 欄位名 型別 first/after xxx;
-
刪除表字段: alter table t1 drop 欄位名;
-
修改表字段: alter table t1 change 原名 新名 新型別;
練習題:
-
建立資料庫mydb4,指定字符集utf8,並使用
create database mydb4 charset=utf8;
use mydb4;
-
建立teacher表,欄位只有名字name,指定字符集utf8
create table teacher(name varchar(50))charset=utf8;
-
新增表字段: 最後面新增age 最前面新增id age前面新增salary工資
alter table teacher add age int;
alter table teacher add id int first;
alter table teacher add salary int after name;
-
刪除age欄位
alter table teacher drop age;
-
修改表名為 t
rename table teacher to t;
-
刪除表
drop table t;
-
刪除資料庫mydb4
drop database mydb4;
資料相關SQL
create database db2 charset=utf8;
use db2;
create table person(id int,name varchar(50),age int)charset=utf8;
-
插入資料
-
全表插入格式: insert into 表名 values(值1,值2,值3);
-
注意此處是values,表示可以插入多條資料,當為value時只能插入一條資料
-
-
指定欄位插入格式: insert into 表名(欄位1名,欄位2名) values(值1,值2);
-
舉例:
insert into person values(1,"tom",18); //全表插入
insert into person(name,age) values('jerry',20); //指定欄位插入,多個欄位之間用,隔開
insert into person(name)values("Lucy");
-
-
批量插入資料: 在values後面寫多組值
insert into person values(2,'lilei',10),(3,'hanmeimei',20);
insert into person(name) values('liubei'),('guanyu');
-
中文問題:
insert into person values(5,'劉德華',50);
-
如果執行上面包含中文的SQL語句出現下面的提示, 執行set names gbk;
-
-
查詢資料
格式: select 欄位資訊 from 表名 where 條件;
舉例:
select name from person; //查詢person表裡面所有的名字
select name from person where age>30;
select name,age from person where name='tom';
select * from person; //查詢所有資料的所有欄位資訊
-
修改資料
-
格式: update 表名 set 欄位名=值,欄位名=值 where 條件;
-
舉例:
update person set age=66 where name='Lucy';
update person set name='張學友' where age=20;
update person set age=100 where age is null;//注意查詢條件的引數為NULL時,要在條件中寫:欄位名 is null
-
-
刪除資料
-
格式: delete from 表名 where 條件;
-
舉例:
delete from person where name='tom';//刪除表中指定條件的資料
delete from person where age<30;
delete from person;//刪除表中所有資料
-
資料相關練習
1. 建立資料庫mydb5 字符集utf8並使用
create database mydb5 charset=utf8;
use mydb5;
2. 建立hero表 欄位有: id ,name , type(型別的意思 字串)
create table hero(id int,name varchar(50),type varchar(10))charset=utf8;
3. 插入以下資料:
insert into hero values
(1,'荊軻','刺客'), (2,'諸葛亮','軍師'),
(3,'關羽','戰士'), (4,'黃忠','射手');
4. 修改諸葛亮為法師
update hero set type='法師' where name='諸葛亮';
5. 給表添加價格money欄位 alter table hero add money int;
6. 修改id小於5的價格為6888
update hero set money=6888 where id<5;
7. 修改法師為打野
update hero set type='打野' where type='法師';
8. 修改黃忠為五虎上將
update hero set name='五虎上將' where name='黃忠';
9. 刪除id等於5的資料
delete from hero where id=5;
11. 修改表名為heros
rename table hero to heros;
12. 刪除money欄位
alter table heros drop money;
13. 刪除所有資料 ,刪除heros表
delete from heros;
drop table heros;
晚課任務:
-
建立汽車car表 欄位有: id ,name , type(型別的意思 字串)
create table car(id int,name varchar(50),type varchar(40))charset=utf8;
-
插入以下資料: 1 五菱巨集光 麵包車 , 2 保時捷911 跑車 , 3 蔚來ES8 SUV, 4 小鵬p7 純電轎車
insert into car values(1,'五菱巨集光','麵包車'),(2,'保時捷911','跑車'),(3,'蔚來ES8','SUV'),(4,'小鵬p7','純電轎車');
-
修改五菱巨集光為敞篷跑車
update car set type='敞篷跑車' where name='五菱巨集光';
-
給表添加價格money欄位
alter table car add money int;
-
修改id小於3的價格為10000
update car set money=10000 where id<3;
-
修改蔚來ES8 為純電SUV
update car set type='純電SUV' where name='蔚來ES8';
-
修改保時捷911價格為20000
update car set money=20000 where name='保時捷911';
-
刪除id等於4的資料
delete from car where id=4;
-
修改所有車的價格為888
update car set money=888;
-
修改表名為cars
rename table car to cars;
-
刪除money欄位
alter table car drop money;
-
刪除所有資料
delete from car;
-
刪除表
drop table car;
-
刪除資料庫
drop database mydb5;
HeidiSQL使用:
開啟頁面:
點選“新建”,出現一個未命名的會話標題,右側輸入使用者名稱:root,密碼:root,埠選擇3306,開啟(由於之前安裝了MySQL佔用了埠3306,此處輸入MariaDB埠號3307)
選擇Yes
生成頁面如下:
進行SQL程式碼編寫:
主機顯示每個資料庫的資訊:
資料庫:顯示當前資料庫下所包含的資料
Table:顯示錶欄位的詳細資訊
資料:查看錶中的資料
查詢:進行SQL程式碼編寫
可通過在顯示頁面上點選資料庫或表進行定位,通過單擊右鍵或者上面的程式碼編輯工具進行對資料庫和表進行操作,左下側對應相應SQL程式碼: