一、相對於表結構上的語法
創建數據庫名稱爲"MysqlTest",分別創建課目表 “topic’,學生表"student”,教師表"teacher",分數表"score"。
(一)庫
1,創建數據庫
create database test;
create database MysqlTest;
2、使用數據庫:use MysqlTest;
3、刪除庫
drop database test;
(二)表
1、分別創建表
create table topic(
`id` int(11) auto_increment not null,
`course` char(32) not null,
`t_id` tinyint(4) not null,
primary key(`id`)
)engine=innodb default charset=utf8;
create table student(
`id` int(11) auto_increment not null,
`s_name` char(50) not null,
`sex` char(16) not null,
`s_id` int(11) not null,
primary key(`id`),
)engine=innodb default charset=utf8;
create table teacher(
`id` int(11) auto_increment not null,
`t_name` char(50) not null,
primary key(`id`)
)engine=innodb default charset=utf8;
create table score(
`id` int(11) auto_increment not null,
`s_name` char(50) not null,
`course` int(11) not null,
`number` int(11) not null,
primary key(`id`)
)engine=innodb default charset=utf8;
create table test(
`id` int(11) auto_increment not null,
`s_test` char(50) not null,
primary key(`id`)
)engine=innodb default charset=utf8;
2、修改表名稱
rename table test to new_test;
2、刪除一張表
drop table new_test;
3、增加一個字段
alter table student add column `test` int(11) default '0' comment "測試字段" not null;
4、修改字段的類型,修改字段的名稱,刪除一個字段。
alter table student modify `test` char(32) not null;
alter table student change `test` `new_test` varchar(55) not null;
alter table student drop `new_test` ;
分別運行展示:
4、索引。
普通索引是可以重複的,唯一索引是不能重複。語句可以用creat或者alter。
- 創建索引
alter table student add index `index_s_name` (`s_name`);
create unique index `index_s_id` on student(`s_id`);
- 刪除索引
drop index `index_s_name` on student;
alter table student drop index `index_s_id`;
5、主鍵。
主鍵是唯一的,不可重複。
其實還是可以修改的,之所以不能刪除是因爲id設置了自增,這裏可以先修改Id的類型,然後在刪除。
alter table student modify `id` int(11);
alter table student drop primary key;
alter table student add primary key(`s_id`);
假如把新的主鍵設置自增,那麼可以
alter table student modify `s_id` int(11) auto_increment;
二、相對與數據上的語法。
1、增加數據
insert into student (`s_name`,`sex`,`s_id`)values("張三","男",123),("王二","男",124),("麻子","男",125),("小美","女",126),("小倩","女",127),("小夏","女",128);
2、修改一條數據
update student set s_name="kangkang" where s_id = 123;
3、刪除一條數據
delete student where s_id = 123;
4、查詢
select s_name from student where s_id > 126;
select s_name,sex from student where s_id > 126;
另外還有一些組合:
去重:select distinct 字段 from 表名 where 條件
邏輯條件: and or
比較條件:< , <=, >, >=, <>, between value1 and value2
判斷空:
1)判斷null: is null
2)判斷空字符串: ="" / <>""
模糊條件:like
%:替換任意長度字符
_:替換單個字符
分頁查詢,limit 起始行,查詢行數
排序:order by 字段 asc/desc
asc:升序
desc:降序
分組:group by 字段
三、經典面試例題
(一)
表如下
1、查詢兩門以上不及格同學的名字,不及格科目名稱及分數。
select sc.s_name as "名字",sc.number as "分數",tp.course as "課目"
from score sc,topic tp
where sc.number < 60 and tp.id = sc.course and sc.s_name
in(select sc.s_name from score sc where number < 60 group by s_name having count(*) >= 2);
select sc.s_name,sc.number,tp.course
from topic tp,score sc,
(select s_name,course,number from score where number < 60 group by s_name having count(*)>=2)sc1
where sc.s_name=sc1.s_name and tp.t_id=sc.course and sc.number<60;
簡單說明一下,group by:
從字面上的意思理解 group是分組,by後面寫字段名,表示根據哪個字段分組,group by 必須根據聚合函數來寫:
- count() 計數
- sun() 求和
- avg() 平均數
- max() 最大值
- min() 最小值
思考:請查詢每科的平均成績?
select tp.course,AVG(sc.number) from topic tp ,score sc
where tp.id = sc.course group by sc.course;
2、查詢每科成績最好的前兩名的學生。
select a.s_name,a.number,tp.course from score a,topic tp
where tp.id = a.course and
(select count(*) from score b where a.course = b.course and b.number >= a.number) <= 2
order by a.course, a.number desc;
3、查詢每科最好成績的學生姓名,課目,分數
select sc1.s_name, sc1.number, tp.course from score sc1,
(select course, Max(number)as maxnumber from score group by course) sc, topic tp
where sc1.number=sc.maxnumber and sc.course=sc1.course and sc.course=tp.id;
各科成績統計參考連接[link:] https://www.cnblogs.com/lsgcoder101/p/6011059.html
四、mysql存儲引擎,常用配置參數,主從配置
1、存儲引擎
在mysql客戶端,使用命令 show engines; 查看MYSQL支持引擎。
最常用的兩種存儲引擎:
- Myisam是Mysql的默認存儲引擎。當create創建新表時,未指定新表的存儲引擎時,默認使用Myisam。每個MyISAM在磁盤上存儲成三個文件。文件名都和表名相同,擴展名分別是.frm(存儲表定義)、.MYD (MYData,存儲數據)、.MYI (MYIndex,存儲索引)。數據文件和索引文件可以放置在不同的目錄,平均分佈io,獲得更快的速度。
- InnoDB存儲引擎提供了具有提交、回滾和崩潰恢復能力的事務安全。但是對比Myisam的存儲引擎,InnoDB寫的處理效率差一些並且會佔用更多的磁盤空間以保留數據和索引
2、常用配置參數
datadir:指定mysql的數據目錄位置,用於存放mysql數據庫文件、日誌文件等。
配置示例:datadir=D:/wamp/mysqldata/Data
default-character-set:mysql服務器默認字符集設置。
配置示例:default-character-set=utf8
skip-grant-tables:當忘記mysql用戶密碼的時候,可以在mysql配置文件中配置該參數,跳過權限表驗證,不需要密碼即可登錄mysql。
3、主從配置
-
比如從庫一個表不存在導致整個從庫的同步都卡住等,可以配置從庫的my.cnf,添加以下配置 :
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
這樣出現這些錯誤代碼時主從複製依然會跳過錯誤繼續同步下一條記錄 -
主庫上有個數據庫不需要同步
如果主庫上有個數據庫不需要同步,可以在主庫的配置文件添加
binlog-ignore-db = testdb -
只同步某一個數據庫
如果只想同步某一個數據庫,可以在配置文件添加
binlog-do-db =testdb
如果二者都不存在,那麼所有數據庫都會被同步,如果二者都存在則以binlog-do-db 優先,如果有多個數據庫,那麼填寫多條記錄即可 -
某一個表不被同步
如果只想某一個表不被同步,那麼配置文件添加
replicate-ignore-table=testdb.test_table即可 -
保留7天二進制日誌
因爲主從複製必須開啓binlog,而binlog很可能日積月累非常大寫爆磁盤,所以可以配置
expire-logs-days = 7
只保留7天的二進制日誌 -
限制binlog空間大小
relay-log-space-limit = 16G
限制binlog的空間最大使用16G,以防磁盤被日誌佔滿