1. 程式人生 > >mysql資料庫(入門)一

mysql資料庫(入門)一

MySQL資料庫

MySQL 是一個關係型資料庫管理系統,由瑞典 MySQL AB 公司開發,目前屬於 Oracle 公司。MySQL 是一種關聯資料庫管理系統,關聯資料庫將資料儲存在不同的表中,而不是將所有資料放在一個大倉庫內,這樣就增加了速度並提高了靈活性。

  • MySQL 是開源的,所以你不需要支付額外的費用。
  • MySQL 支援大型的資料庫。可以處理擁有上千萬條記錄的大型資料庫。
  • MySQL 使用標準的SQL資料語言形式。
  • MySQL 可以運行於多個系統上,並且支援多種語言。這些程式語言包括C、C++、Python、Java、Perl、PHP、Eiffel、Ruby和Tcl等。
  • MySQL 對PHP有很好的支援,PHP是目前最流行的Web開發語言。
  • MySQL 支援大型資料庫,支援5000萬條記錄的資料倉庫,32位系統表文件最大可支援4GB,64位系統支援最大的表文件為8TB。
  • MySQL 是可以定製的,採用了GPL協議,你可以修改原始碼來開發自己的 MySQL 系統。

資料庫相關的SQL語句(基礎)

1.查詢所有資料庫
show databases
2. 建立資料庫
creste database 資料庫名稱
3. 查詢資料庫詳情 檢視資料庫的字符集
show create database 資料庫名稱
4.建立資料庫指定字符集
create database 資料庫名稱 character set 字符集(例如gbk)
5.刪除資料庫
drop database 資料庫名稱
6.選中資料庫
use 資料庫名稱

表相關sql(一定要選中要操作的資料庫)

1.建立表格式
create table 表名(欄位名 型別,欄位名 型別…)
2.查詢所有表
show tables
3.查詢表詳情
show create table 表名
4.建立表指定引擎和字符集
create table 表名(欄位名 型別…) engine=innodb(預設)/myisam,charset=gbk/utf-8

  • innodb(預設):支援資料庫的高階操作如:事務,外來鍵等
  • myisam:不支援高階操作,只支援基礎的增刪改查操作。

5.查看錶結構(表字段)
desc 表名;
6.刪除表
drop table 表名

修改表相關

建立一個表以此為例

修改表相關

create table student(name varchar(10),age int);
  1. 修改表名 -格式: rename table 原名 to 新名; rename table student to stu;
  2. 修改表引擎和字符集 -格式: alter table 表名 engine=myisam/innodb charset=gbk/utf8;

    alter table stu engine=myisam charset=gbk;

  3. 新增表字段
  4. 最後新增 alter table 表名 add 欄位名 型別; alter table stu add chinese int;
  5. 最前面新增 alter table 表名 add 欄位名 型別 first; alter table stu add math int first;
  6. 某個欄位後面新增 alter table 表名 add 欄位名 型別 after xxx; alter table stu add english int after name;
  7. 刪除表字段 -格式:alter table 表名 drop 欄位名; alter table stu drop english;
  8. 修改表字段名稱和型別 -格式:alter table 表名 change 原欄位名 新欄位名 新型別; alter table stu change math english int;
  9. 修改欄位型別和位置 -格式: alter table 表名 modify 欄位名 型別 first/(after xxx); alter table stu modify english int after chinese;
  10. 練習:
  11. 建立一個hero表 有 id 整數 name 字串 type 字串 三個欄位 create table hero(id int,name varchar(10),type varchar(10));
  12. 修改hero表的屬性引擎為myisam 字符集為gbk alter table hero engine=myisam charset=gbk;
  13. 給hero表新增money欄位 整數型別,新增在name的後面 alter table hero add money int after name;
  14. 修改type欄位 名稱改為 herotype varchar(20) alter table hero change type herotype varchar(20);
  15. 修改表名hero為 heros rename table hero to heros;
  16. 修改name欄位到最後面 alter table heros modify name varchar(10) after hero_type;
  17. 刪除money欄位 alter table heros drop money;
  18. 刪除hero表 drop table heros;

資料相關SQL

create table hero(name varchar(10),age int);
  1. 插入資料 -全表插入格式:insert into 表名 values(值1,值2...); insert into hero values('李白',30); -指定欄位格式:insert into 表名 (欄位1名,欄位2名) values(值1,值2); insert into hero (name) values('關羽');

    • 批量插入資料: insert into hero values('劉備',20),('關羽',19),('張飛',30); insert into hero (name) values('悟空'),('八戒'),('沙僧');
  2. 查詢資料 *代表所有欄位 select * from hero; select name from hero; select name,age from hero; select * from hero where age<20; select * from hero where name='關羽';

  3. 刪除資料 -格式:delete from 表名 where age=10; -刪除表中所有資料: delete from 表名; delete from hero where age=20; delete from hero where name='悟空';
  4. 修改資料 -格式:update 表名 set name='Tom' where age=20; update hero set age=100 where name='八戒'; update hero set age=50;

中文問題

在命令列中執行 set names gbk; (需要在資料庫裡面執行)

顯示?的是因為資料庫或表的字元編碼為gbk 修改為utf8