Oracle
、MySQL
、PostgreSQL
、SQL Server
、DB2
等;NoSql
、MongoDB
、Cloudant
、Redis
、Elasticsearch
、HBase
等;優勢:html
缺點:mysql
優勢:sql
缺點:數據庫
MySQL在過去因爲性能高、成本低、可靠性好,已經成爲最流行的開源數據庫,所以被普遍地應用在Internet上的中小型網站中。隨着MySQL的不斷成熟,它也逐漸用於更多大規模網站和應用,好比維基百科、Google和Facebook等網站。很是流行的開源軟件組合LAMP中的「M」指的就是MySQL。服務器
tinyint
、smallint
、mediumint
、int
、bigint
;float
、double
、real
、decimal
;date
、time
、datetime
、timestamp
、year
;char
、varchar
;tinytext
、text
、mediumtext
、longtext
;tinyblob
、blob
、mediumblob
、longblob
;unsigned
、not null
、default
;CREATE
、ALTER
、DROP
;# 建立庫 create database Student; # 若是沒有Student這個庫則建立它,character set指定字符集爲utf8, 排序規則爲utf8_bin create datebase if not exists Student character set utf8 collate utf8_bin # 刪除庫 drop database Student; # 修改庫 alter database Student character set = utf8mb4 alter database Student collate = utf8mb4_general_ci # 建立表 create table students (id int not null primary key auto_increment, name varchar(250) not null, class varchar(250) not null) engine=InnoDB default charset=utf8; # 查看錶結構 desc students; # 修改表 alter table students add gender enum('f','m'); alter table students drop gender; alter table students change name username varchar(100) after id; alter table students modify username varchar(100) first; # 刪除表 drop table students;
INSERT
、DELETE
、UPDATE
、SELECT
;# 給students表增長數據 insert into students (class,username) values ('一年級', 'Luky'), ('二年級', 'Tom'), ('三年級', 'Mark'); # 修改students表中id = 1 的字段class的值爲「初一」 update students set class = '初一' where id = 1; # 刪除class爲一年級的字段 delete from students where class = '一年級';
GRANT
、REVOKE
;# 先建立用戶,再受權 create user Mark@'172.16.19.%' identified by '123456'; grant all on *.* to Mark@'172.16.19.%'; flush privileges; # 建立用戶的同時給用戶受權 grant all on *.* to Mark@'172.16.19.%' identified by '123456'; flush privileges; # 給用戶授予某些權限 show grants for Mark@'172.16.19.%'; # 撤銷受權 revoke select ON *.* from Mark@'172.16.19.%'; flush privileges; # 查看用戶的權限 show grants for Mark@'172.16.19.%'; # 刪除用戶 delete from mysql.user where user = "Mark"; flush privileges; # 刪除用戶 drop user 'Mark'@'192.168.%.%'; # 修改密碼 (1)set password for 'Mark'@'192.168.%.%' = password('123456'); (2)使用update命令,直接修改 mysql.user 用戶表,修改以後flush priveleges;
commit(提交)
、rollback(回滾)
;# SQL刪除記錄語句 sql = "DELETE FROM Student WHERE AGE > %s" % (30) try: cursor.execute(sql) # 向數據庫提交 db.commit() except: # 發生錯誤時回滾 db.rollback()
\G
結尾表示以以豎行顯示select * from Student
select id,name from Student;
select id as num,name from Student; select id as num,name as username from Student;
+, -, * ,/ ,= ,!= ,<=, >=
,鏈接詞有:and , or
;select * from Student where id >= 1; select * from Student where id <= 5 and id >1; select * from Student where id <3 or id >10; select * from Student where id between 1 and 5; # between 較小的數 and 較大的數
%
表示任意長度的字符,_
表示任意單個字符select * from Student where class like 'Ma%'; select * from Student where class like 'Ma_k';
select * from Student where id is not null; select * from Student where id is null;
avg()
, max()
, min()
, count()
,sum()
;select age,gender from students group by gender; select avg(age),gender from students group by gender; select min(age), gender from students group by gender; select max(age), gender from students group by gender; select count(id), gender from students group by gender;
select * from Student order by id desc;
select avg(age) as 'average_age', gender from students group by gender having average_age > 50;
# 選前5行 select id,name from students order by id desc limit 5; # 前4個不選,從第5行開始選2行 select id,name from students order by id limit 4, 2;
where -> group by -> having -> order by -> limit
select *,avg(score) as '各班平均成績' from students where id > 1 group by class having avg(score) > 55 order by score desc limit 3 ;
參考:https://www.9xkd.com/user/plan-view.html?id=4200717020數據結構