python學習之數據庫mariadb操做

數據庫簡介
數據庫分類
關係型數據庫:指採用了關係模型來組織數據的數據庫。關係模型指的就是二維表格模型,而一
個關係型數據庫就是由二維表及其之間的聯繫所組成的一個數據組織。主流的關係型數據庫有:
Oracle、Microsoft SQL Server、MySQL、PostgreSQL,SQLite、MariaDB(MySQL的一個分
支)Microsoft Access、SAP。
非關係型數據庫:指非關係型的,分佈式的,以鍵值對存儲且結構不固定,能夠減小一些
時間和空間的開銷。非關係型數據庫都是針對某些特定的應用需求,主要分爲如下幾類:
1). 面向海量數據訪問的面向文檔數據庫:MongoDB、Amazon DynamoDB、Couchbase等。
2). 面向高性能併發讀寫的key-value數據庫: Redis、 Memcached等。
3). 面向搜索數據內容的搜索引擎:Elasticsearch,Splunk,Solr,MarkLogic和Sphinx等。
4). 面向可擴展性的分佈式數據庫:Cassandra,HBase等。
當前物理的數據庫都是按照E-R模型進行設計的,
• E表示entry,實體
• R表示relationship,關係
• 一個實體轉換爲數據庫中的一個表
關係描述兩個實體之間的對應規則,包括: 一對一 ,一對多, 多對多
python學習之數據庫mariadb操做
通過研究和對使用中問題的總結,對於設計數據庫提出了一些規範,這些規範被稱爲範式
• 第一範式(1NF):列不可拆分 , 即無重複的域。
• 第二範式(2NF):惟一標識 ,即擁有實體的惟一標識(eg: 身份證、id號等)。
• 第三範式(3NF):引用主鍵 ,即每列數據都與主鍵直接相關。
說明:關係型數據庫有六種範式。通常說來,數據庫只需知足第三範式(3NF)就好了。
MySQL經常使用存儲引擎分析
數據庫存儲引擎是數據庫底層軟件組織,進行建立、查詢、更新和刪除數據。不一樣的存儲引
擎提供不一樣的存儲機制、索引技巧、鎖定水平等功能, MySQL的核心就是存儲引擎。
MySQL查詢存儲引擎SQL語句:SHOW ENGINES
安裝
python學習之數據庫mariadb操做
數據庫服務管理
python學習之數據庫mariadb操做
安全性密碼設置
python學習之數據庫mariadb操做python

  1. 關閉mysql服務器的防火牆
    python學習之數據庫mariadb操做
  2. 用戶受權: 容許root用戶經過westos密碼 在任意主機(%)遠程登錄並操做數據庫;
    python學習之數據庫mariadb操做
    容許遠程鏈接
    python學習之數據庫mariadb操做
    找回密碼
    python學習之數據庫mariadb操做
    數據庫操做
    python學習之數據庫mariadb操做
    表操做
    python學習之數據庫mariadb操做
    表建立: 數據完整性
    • 一個數據庫就是一個完整的業務單元,能夠包含多張表,數據被存儲在表中
    • 在表中爲了更加準確的存儲數據,保證數據的正確有效,能夠在建立表的時候,爲
    表添加一些強制性的驗證, 包括數據字段的類型、約束
    在mysql中包含的數據類型不少,這裏主要列出來經常使用的幾種:
    • 數字:int,decimal, float
    • 字符串:varchar,text
    • 日期:datetime
    • 布爾:bool
    表的建立: 約束
    • 主鍵 primary key
    • 非空 not null
    • 唯一 unique
    • 默認 default
    • 外鍵 foreign key
    • 自動增加 auto_increment
    數據操做
    python學習之數據庫mariadb操做
    備份與恢復
    python學習之數據庫mariadb操做
    查詢的基本語法
    select from 表名;
    • from關鍵字後面寫表名,表示數據來源因而這張表
    • select後面寫表中的列名,若是是
    表示在結果中顯示錶中全部列
    • 在select後面的列名部分,可使用as爲列起別名,這個別名出如今結果集中
    • 若是要查詢多個列,之間使用逗號分隔
    消除重複行
    在select後面列前使用distinct能夠消除重複的行
    select distinct gender from students;
    條件
    使用where子句對錶中的數據篩選,結果爲true的行會出如今結果集中
    select * from 表名 where 條件;
    優先級
    •小括號,not,比較運算符,邏輯運算符
    •and比or先運算,若是同時出現並但願先算or,須要結合()使用
    python學習之數據庫mariadb操做
    分組
    • 按照字段分組,表示此字段相同的數據會被放到一個組中
    • 分組後,只能查詢出相同的數據列,對於有差別的數據列沒法出如今結果集中
    • 能夠對分組後的數據進行統計,作聚合運算
    select 列1,列2,聚合... from 表名 group by 列1,列2,列3...
    分組後的數據篩選
    having後面的條件運算符與where的相同
    對比where與having
    • where是對from後面指定的表進行數據篩選,屬於對原始數據的篩選
    • having是對group by的結果進行篩選
    聚合
    爲了快速獲得統計數據,提供了5個聚合函數
    python學習之數據庫mariadb操做
    排序
    爲了方便查看數據,能夠對數據進行排序:
    • 將行數據按照列1進行排序,若是某些行列1的值相同時,則按照列2排序,以此類推
    • 默認按照列值從小到大排列
    • asc從小到大排列,即升序, desc從大到小排序,即降序
    獲取部分行
    當數據量過大時,在一頁中查看數據是一件很是麻煩的事情:
    • 從start開始,獲取count條數據
    • start索引從0開始
    實驗: 客戶端能夠遠程鏈接服務端數據庫mysql

    • 客戶端: 172.25.254.197
    • 服務端: 172.25.254.18
      服務端操做:
      $ mysql -uroot -p
      #查看mysql數據庫中user數據庫表的三列內容: Host,User,Password.
      MariaDB [(none)]> select Host,User,Password from mysql.user;
      #172.25.254.197這臺主機以root用戶身份遠程登陸, 密碼爲westos, 訪問數據庫的全部內容(.)
      MariaDB [(none)]> grant all on . to root@'172.25.254.197' identified by 'westos';
      #任意一臺主機以root用戶身份遠程登陸, 密碼爲westos, 訪問數據庫的全部內容(.)
      #sql語句中的%等價於.
      MariaDB [(none)]> grant all on
      . to root@'%' identified by 'westos';
      #任意一臺主機以root用戶身份遠程登陸, 密碼爲westos, 訪問mysql數據庫的全部表(mysql.
      )
      MariaDB [(none)]> grant all on mysql.* to root@'%' identified by 'westos';
      MariaDB [(none)]> select Host,User,Password from mysql.user;

    #刪除用戶受權(遠程登陸)
    MariaDB [(none)]> drop user root@'%';sql

客戶端測試:
#指定主機名爲172.25.254.18, 用戶名爲root遠程登陸.
$ mysql -h 172.25.254.18 -uroot -pwestos
#建立數據庫Blog並指定編碼格式爲utf8(存儲的數據爲中文, 須要設置);
MariaDB [(none)]> create database Blog default charset='utf8';
#顯示全部數據庫名稱;
MariaDB [(none)]> show databases;
#選擇/切換數據庫Blog;
MariaDB [(none)]> use Blog;
#查看當前選擇的數據庫;
MariaDB [Blog]> select database();
#查看當前數據庫的全部數據庫表;
MariaDB [Blog]> show tables;
#建立數據庫表userinfo, 兩列數據。
#varchar可變長字符串, not null數據非空,unique數據惟一。
MariaDB [Blog]> create table userinfo(
-> username varchar(20) not null unique,
-> password varchar(20) not null)
-> ;
#查看錶結構
MariaDB [Blog]> desc userinfo;
#添加數據到數據表中;
MariaDB [Blog]> insert into userinfo values('user2', 'passwd');
MariaDB [Blog]> insert into userinfo values('張三', 'passwd');
#數據查詢;
MariaDB [Blog]> select * from userinfo;
#修改表結構: 添加一列信息、修改一列信息、刪除一列信息.
MariaDB [Blog]> alter table userinfo add gender varchar(3);
MariaDB [Blog]> alter table userinfo change gender sex varchar(3);
MariaDB [Blog]> alter table userinfo drop sex;
#數據表重命名;
MariaDB [Blog]> rename table userinfo to users;
#查看錶的常見語句;
MariaDB [Blog]> show create table users;
#刪除數據表;
MariaDB [Blog]> drop table users;
#刪除數據庫;
MariaDB [Blog]> drop database Blog;
MariaDB [(none)]> create database Blog default charset='utf8';
MariaDB [(none)]> use Blog;
MariaDB [Blog]> create table users(
-> id int primary key auto_increment,
-> username varchar(20) unique not null,
-> password varchar(20) not null default '000000');
MariaDB [Blog]> desc users;
MariaDB [Blog]> insert into users values(1, 'user1', 'password');
MariaDB [Blog]> insert into users(username) values('user2');
MariaDB [Blog]> insert into users(username) values('user3'),('user4'), ('user5');
MariaDB [Blog]> update users set password='666666' where username='user4';數據庫

MariaDB [Blog]> select from users where username='user4';
MariaDB [Blog]> select
from users;
MariaDB [Blog]> delete from users where username='user4';
MariaDB [Blog]> select * from users;
create table student(
sno varchar(12) primary key,
sname varchar (10) comment '學生姓名',
sex varchar (2) comment '性別',
age int,
address varchar(50),
classno varchar (5)
);
#*****關於查詢條件****安全

一、 查詢students表中的全部記錄的sname、ssex和class列。
MariaDB [Blog]> select sname,ssex,class from students;服務器

二、 查詢教師全部的單位即不重複的Depart列。
MariaDB [Blog]> select distinct depart from teachers;併發

三、 查詢students表的全部記錄。
MariaDB [Blog]> select * from students;分佈式

四、 查詢scores表中成績在60到80之間的全部記錄。
MariaDB [Blog]> select * from scores where degree between 60 and 80;ide

五、 查詢scores表中成績爲85,86或88的記錄。
MariaDB [Blog]> select from scores where degree=85 or degree=86 or degree=88;
MariaDB [Blog]> select
from scores where degree in (85,86,88);函數

六、 查詢students表中「95031」班或性別爲「女」的同窗記錄。(做業)
select * from students where xxxxxx or xxxxx;

*關於排序***
七、 以class降序查詢students表的全部記錄。
MariaDB [Blog]> select from students order by class desc;
MariaDB [Blog]> select
from students order by class;(默認升序)

八、 以cno升序、degree降序查詢scores表的全部記錄。
以cno升序、degree降序: 當cno相同時, 按照degree降序排列。
cno degree
1 3
1 2
2 3
MariaDB [Blog]> select * from scores order by cno,degree desc;

****關於聚合函數*****
九、 查詢「95031」班的學生人數。
MariaDB [Blog]> select from students where class='95031';
MariaDB [Blog]> select count(
) from students where class='95031';
MariaDB [Blog]> select count(*) as studentCount from students where class='95031'; (最終版)

十、查詢‘3-105’號課程的平均分。
MariaDB [Blog]> select avg(degree) as avgScore from scores where cno='3-105';

***關於group by 和having*
十一、查詢scores表中至少有5名學生選修的並以3開頭的課程的平均分數。
十二、查詢最低分大於70,最高分小於90的Sno列。

1三、查詢scores表中的最高分的學生學號和課程號。
1四、查詢全部學生的Sname、Cno和Degree列。
1五、查詢全部學生的Sno、Cname和Degree列。
1六、查詢全部學生的Sname、Cname和Degree列。
1七、查詢「95033」班所選課程的平均分。
USE Blog;

CREATE TABLE IF NOT EXISTS students
(sno VARCHAR(3) NOT NULL,
sname VARCHAR(4) NOT NULL,
ssex VARCHAR(2) NOT NULL,
sbirthday DATETIME,
class VARCHAR(5));

CREATE TABLE IF NOT EXISTS courses
(cno VARCHAR(5) NOT NULL,
cname VARCHAR(10) NOT NULL,
tno VARCHAR(10) NOT NULL);

CREATE TABLE IF NOT EXISTS scores
(sno VARCHAR(3) NOT NULL,
cno VARCHAR(5) NOT NULL,
degree NUMERIC(10, 1) NOT NULL);

CREATE TABLE IF NOT EXISTS teachers
(tno VARCHAR(3) NOT NULL,
tname VARCHAR(4) NOT NULL, tsex VARCHAR(2) NOT NULL,
tbirthday DATETIME NOT NULL, prof VARCHAR(6),
depart VARCHAR(10) NOT NULL);

INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (108 ,'曾華' ,'男' ,'1977-09-01',95033);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (105 ,'匡明' ,'男' ,'1975-10-02',95031);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (107 ,'王麗' ,'女' ,'1976-01-23',95033);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (101 ,'李軍' ,'男' ,'1976-02-20',95033);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (109 ,'王芳' ,'女' ,'1975-02-10',95031);
INSERT INTO students (sno,sname,ssex,sbirthday,class) VALUES (103 ,'陸君' ,'男' ,'1974-06-03',95031);

INSERT INTO courses(cno,cname,tno)VALUES ('3-105' ,'計算機導論',825);
INSERT INTO courses(cno,cname,tno)VALUES ('3-245' ,'操做系統' ,804);
INSERT INTO courses(cno,cname,tno)VALUES ('6-166' ,'數據電路' ,856);
INSERT INTO courses(cno,cname,tno)VALUES ('9-888' ,'高等數學' ,100);

INSERT INTO scores(sno,cno,degree)VALUES (103,'3-245',86);
INSERT INTO scores(sno,cno,degree)VALUES (105,'3-245',75);
INSERT INTO scores(sno,cno,degree)VALUES (109,'3-245',68);
INSERT INTO scores(sno,cno,degree)VALUES (103,'3-105',92);
INSERT INTO scores(sno,cno,degree)VALUES (105,'3-105',88);
INSERT INTO scores(sno,cno,degree)VALUES (109,'3-105',76);
INSERT INTO scores(sno,cno,degree)VALUES (101,'3-105',64);
INSERT INTO scores(sno,cno,degree)VALUES (107,'3-105',91);
INSERT INTO scores(sno,cno,degree)VALUES (108,'3-105',78);
INSERT INTO scores(sno,cno,degree)VALUES (101,'6-166',85);
INSERT INTO scores(sno,cno,degree)VALUES (107,'6-106',79);
INSERT INTO scores(sno,cno,degree)VALUES (108,'6-166',81);

INSERT INTO teachers(tno,tname,tsex,tbirthday,prof,depart) VALUES (804,'李誠','男','1958-12-02','副教授','計算機系');INSERT INTO teachers(tno,tname,tsex,tbirthday,prof,depart) VALUES (856,'張旭','男','1969-03-12','講師','電子工程系');INSERT INTO teachers(tno,tname,tsex,tbirthday,prof,depart) VALUES (825,'王萍','女','1972-05-05','助教','計算機系');INSERT INTO teachers(tno,tname,tsex,tbirthday,prof,depart) VALUES (831,'劉冰','女','1977-08-14','助教','電子工程系');

相關文章
相關標籤/搜索