MySQL的基本使用

2021-10-23 04:45:21 字數 4614 閱讀 3605

1.資料庫:資料庫就是一種特殊的檔案,其中儲存著需要的資料

2.資料庫分為關係型資料庫和非關係型資料庫

關係型資料庫:

oracle、db2、microsoft sql server、microsoft access、mysql

非關係型資料庫:

nosql、cloudant、mongodb、redis、hbase

補充: oracle:在以前的大型專案中使用,銀行,電信等專案

mysql:web時代使用最廣泛的關係型資料庫

ms sql server:在微軟的專案中使用

sqlite:輕量級資料庫,主要應用在移動平台

關係型資料庫的特點:

1)關係型資料庫,是指採用了關係模型來組織資料的資料庫

2)關係型資料庫的最大特點就是事務的一致性

關係型資料庫的優點

1)容易理解:二維表結構是非常貼近邏輯世界乙個概念,關係模型相對網狀、層次等其他模型來說 更容易理解

2)使用方便:通用的sql語言使得操作關係型資料庫非常方便;

3)易於維護:豐富的完整性(實體完整性、參照完整性和使用者定義的完整性)大大減低了資料冗餘和資料不一致的概率;

4)支援sql,可用於複雜的查詢。

關係型資料庫的缺點

1)為了維護一致性所付出的巨大代價就是其讀寫效能比較差;

2)固定的表結構;

3)高併發讀寫需求(高併發讀寫需求,傳統關係型資料庫來說,硬碟i/o是乙個很大的瓶頸。);

4)海量資料的高效率讀寫;

非關係型資料庫

非關係型資料庫的特性

1、使用鍵值對儲存資料;

2、分布式;

3、一般不支援acid特性;

4、非關係型資料庫嚴格上不是一種資料庫,應該是一種資料結構化儲存方法的集合。

非關係型資料庫的優點

1)無需經過sql層的解析,讀寫效能很高;

2)基於鍵值對,資料沒有耦合性,容易擴充套件;

非關係型資料庫的缺點

1)不提供sql支援,學習和使用成本較高;

2)無事務處理,附加功能bi和報表等支援也不好;

acid:

資料庫事務必須具備acid特性,acid分別是 atomic原子性,consistency一致性,

isolation隔離性,durability永續性。

3.sql:sql是結構化查詢語言,是一種用來操作rdbms的資料庫語言,當前關係型資料庫都支援使用sql語言進行操作。

主要的sql語句:

dql:資料查詢語言,用於對資料進行查詢,如select

dml:資料操作語言,對資料進行增加、修改、刪除,如insert、udpate、delete

ddl:資料定義語言,進行資料庫、表的管理等,如create、drop

4.mysql:關係型資料庫

5.shell-mysql:

(1)命令列的連線:開啟終端 執行命令 (mysql -uroot -ppython)注:root為使用者   python 是使用者

(2)退出登入: exit/ ctrl + d/quit

(3)登入成功後,輸入如下命令檢視效果

檢視版本:select version();

6.資料庫的操作

(1)檢視資料庫:show databases;

(2) 使用資料庫: use 資料庫的名字;

(3) 檢視當前使用的資料庫: select database();

(4)建立資料庫: create database 資料庫名 charset=utf8;

例:create database python charset=utf8;

(5)刪除資料庫:drop database 資料庫的名字

例:drop database python;

7.資料表的操作

(1)檢視當前資料庫中所有表:show tables;

(2)檢視表結構: desc 表名;

(3) 建立表:

(auto_increment表示自動增長)

create table table_name(

column1 datatype contrai,

column2 datatype,

column3 datatype,

.....

columnn datatype,

primary key(one or more columns));

例:建立班級表

create table classes(

id int unsigned auto_increment primary key not null,

name varchar(10));

建立學生表

create table students(

id int unsigned auto_increment primary key not null,

name verchar(20) default " ",

age tinyint unsigned defult 0,

height decimal(5,2),

gender enum("男",「女」,「人妖」,『保密』),

cls_id int unsigned default 0

)(4)修改表-新增字段

alter table 表名 add 列名 型別;

例:alter table students add birthday datetime;

(5) 修改表-修改字段:重新命名版

alter table 表名 change 原名 新名 型別以及約束;

例:alter table students change birthday birth datetime not null;

(6)修改表-修改字段:不重新命名版

alter table 表名 modify 列名 型別及約束;

例:alter table students modify birth date not null;

(7)修改表-刪除字段

alter table 表名 drop 列名;

例:alter table students drop birthday;

(8)刪除表

drop table 表名;

例:drop table students;

(9)檢視表的建立語句

show create table 表名;

例:show create table classes;

8.資料的增刪改查(curd)

curd的解釋: 代表建立(create)、更新(update)、讀取(retrieve)和刪除(delete)

(1)查詢基本使用

查詢所有列

select * from 表名;

例:select * from classes;

查詢指定列(可以使用as為列或表指定別名)

select 列1,列2,... from 表名;

例:select id,name from classes;

(2)增加

全列插入:值的順序與表中字段的順序對應

insert into 表名 values(...)

例:insert into students values(0,』郭靖『,1,'蒙古','2016-1-2');

部分列插入:值的順序與給出的列順序對應

insert into 表名(列1,...) values(值1,...)

例:insert into students(name,hometown,birthday) values('黃蓉','桃花島','2016-3-2');

全列多行插入:值的順序與給出的列順序對應

insert into 表名 values(...),(...)...;

例:insert into classes values(0,'python1'),(0,'python2');

insert into 表名(列1,...) values(值1,...),(值1,...)...;

例:insert into students(name) values('楊康'),('楊過'),('小龍女');

(3)修改

update 表名 set 列1=值1,列2=值2... where 條件

例:update students set gender=0,hometown='北京' where id=5;

(4)刪除

delete from 表名 where 條件

例:delete from students where id=5;

邏輯刪除,本質就是修改操作

update students set isdelete=1 where id=1;

(5)資料備份 、恢復

備份

執行mysqldump命令

mysqldump –uroot –p 資料庫名 > python.sql;

按提示輸入mysql的密碼

恢復mysql -uroot -p 資料庫名 < python.sql

mysql 使用 MySQL 基本使用

資料庫 create database 名字 建立資料庫 show databases 檢視所有資料庫 show create database book g 檢視建立好的資料庫的定義 drop database if exists 名字 刪除資料庫 use 名字 使用資料庫 引擎 show eng...

MySQL的基本使用

1 建立 刪除 選擇資料庫 create database 資料庫名 drop database 資料庫名 use 資料庫名 show tables 2 建立 檢視 刪除表 資料庫名 表名 欄位名使用反引號包裹,姓名 男等字串使用單引號包裹。create table student id int u...

MySQL的基本使用

sql ddl 資料定義語言 create drop alter dml 資料操作語言 select insert update delete dcl 資料控制語言 grant revoke commit rollback 最基礎的 建庫 建表 show databases use 資料庫名稱 cr...