MySQL 是一個 DBMS(數據庫管理系統),由瑞典 MySQLAB 公司開發,目前屬於 Oracle 公司,MySQL 是最流行的關係型數據庫管理系統(關係數據庫,是創建在關係數據庫模型基礎上的數據庫,藉助於集合代數等概念和方法來處理數據庫中的數據)。因爲其體積小、速度快、整體擁有成本低,尤爲是開放源碼這一特色,通常中小型網站的開發者都選擇 MySQL 做爲網站數據庫。MySQL 使用 SQL 語言進行操做。mysql
數據庫分爲關係型數據庫和非關係型數據庫。sql
mysql 下載地址: MySQL Community Server 5.7.20數據庫
windows安裝
安裝很簡單,直接點擊下一步,安裝完成後,能夠在服務裏面(win+R而後輸入services.msc)找到mysql服務,能夠手動中止或關閉。ubuntu
ubuntu安裝vim
# 安裝 MySQL 服務端、核心程序 sudo apt-get install mysql-server # 安裝 MySQL 客戶端 sudo apt-get install mysql-client # 安裝結束後,用命令驗證是否安裝並啓動成功 sudo netstat -tap | grep mysql # 能夠根據本身的需求,用 vim 修改 MySQL 的配置文件(my.cnf),使用如下命令 sudo vim /etc/mysql/my.cnf # 啓動 MySQL 服務 sudo service mysql start # 使用 root 用戶登陸,實驗樓環境的密碼爲空,直接回車就能夠登陸 mysql -u root -p
中止mysql進程windows
ps -ef #查看全部進程 ps -ef | grep mysql # 篩選mysql kill 4299 # 刪除進程號爲4299的進程
其餘操做學習
mysqladmin --version # 查看mysql版本 mysqladmin -u root password `new_password`; #建立密碼 # mysql數據庫登錄 mysql -h 'remote_ip' -u username -p # 以後會提示輸入密碼
SELECT column_name, column_name from table_name [WHERE Clause] [LIMIT N][OFFSET M] select * from student limit 10 offset 2; select * from student where id>10; select * from student where register_date > `2016-03-04`; select * from student where register_date like `2016-04%`
insert into student (name, sex, age, register_date) values (`alex`, `M`, 23, `2017-07-12` );
update student set name=`chenronghua`, age=33 where id=4;
delete from stuent where name=`chenronghua`
select * from student order by id desc;
select name,count(*) from student group by name; select register_date,count(*) from student group by register_date; select register_date,count(*) as stu_num from student group by register_date; select name,sum(age) as stu_num from student group by register_date; select coalesce(name,`Total Age`),sum(age) from student group by name with rollup;
alter table study_record modify id int auto_increment; alter table student modify sex enum(`F`,`M`) not null;
alter table student change sex gender char(32) not null default `F`;
create table `study_record`(`id` int(4) auto_increment, `day` int(11) not null, `stu_id` int(11) not null, `status` char(32) not null, primary key (`id`), key `fk_student_key` (`stu_id`), constraint `fk_studnet_key` foreign key (`stu_id`) references `student` (`id`));
先建立兩個表A和B,而且增長一些數據網站
create table A(a int not null); create table B(b int not null); insert into A (a) values (1); insert into A (a) values (2); insert into A (a) values (3); insert into A (a) values (4); insert into B (b) values (3); insert into B (b) values (4); insert into B (b) values (5); insert into B (b) values (6); insert into B (b) values (7);
求A和B表格的交集,並集,差集等。code
RIGHT JOIN(右鏈接): 與 LEFT JOIN 相反,用於獲取右表全部記錄,即便左表沒有對應匹配的記錄。server
select * from A inner join B on A.a = B.b select A.*,B.* from A,B where A.a = B.b
select * from A left join B on A.a = B.b select * from A right join B on A.a = B.b
select * from A full join B on A.a = B.b # 出錯 -- # 能夠經過下面的語法間接支持 select * from A left join B on A.a=B.b union select * from A right join B on A.a=B.b
begin; # 開始事務 rollback; # 回滾 commit; # 提交
-- 已經建立的表增長索引 create index name_index on student (name(32)) -- alter student add index date_index on (register_date(32)) -- 建立表的時候增長索引
drop index index_name on student; show index from student; -- 顯示index
-- 已經建立的表增長索引 create unique index name_index on student (name(32))