Mysql系列一:SQL入門

連接數據庫:
1、在dos窗口下,進入數據庫的安裝目錄的bin目錄下,使用mysqld命令啓動數據庫服務,或者在計算機的服務裏面啓動mysql服務
2、另外打開一個dos窗口,進入數據庫的安裝目錄的bin目錄下,使用命令連接數據庫服務器:mysql -u root -p 

一、數據庫的創建、修改、備份、恢復

創建一個名稱爲mydb1的數據庫
create database mydb1;
show databases;

創建一個使用utf-8字符集的mydb2數據庫。
create database mydb2 character set utf8;

創建一個使用utf-8字符集,並帶校對規則的mydb3數據庫。
create database mydb3 character set utf8 collate utf8_general_ci;

查看前面創建的mydb2數據庫的定義信息
show create database mydb2;

刪除前面創建的mydb1數據庫
drop database mydb1;

查看服務器中的數據庫,並把其中某一個庫的字符集修改爲gb2312;
alter database mydb2 character set gb2312;
show create database mydb2;

演示恢復和備份
create database tt;
use tt;
create table a
(
    name varchar(20)
);
insert into a(name) values('aaaa');
select * from a;
-----看到a表有數據

對tt作備份操作,啓動一個window命令行窗口,進入數據庫的安裝目錄的bin目錄下,執行如下命令
mysqldump -uroot -p tt>c:\tt.sql


演示恢復
1.先刪除庫
drop database tt;

2.恢復tt庫(1)
  2.1  爲恢復庫,要先創建庫  create database tt;
  2.2  再恢復tt庫 
    use tt; 
    source c:\tt.sql        (source:可以執行一個 sql腳本)
    

3.恢復tt庫(2)
  2.1  爲恢復庫,要先創建庫  create database tt;
  2.2  恢復庫   mysql -uroot -proot tt<c:\1.sql;   (window命令)

二、創建表 對錶結構進行修改

創建一個員工表
use mydb2;
create table employee
(
    id int,
    name varchar(40),
    sex varchar(4),
    birthday date,
    entry_date date,
    job varchar(40),
    salary decimal(8,2),
    resume text
);

show tables;  查看庫的所有表
show create table employee;   查看錶的創建細節
desc employee;     看錶結構


在上面員工表的基本上增加一個image列。
alter table employee add image blob;

修改job列,使其長度爲60。
alter table employee modify job varchar(60);

刪除sex列
alter table employee drop sex;

表名改爲user。
rename table employee to user;

修改表的字符集爲utf-8
alter table user character set utf8;

列名name修改爲username
alter table user change column name username varchar(40);

刪除表
drop table user;

三、插入語句

使用insert語句向表中插入三個員工的信息。(插入的字符和日期類型數據應該加上單引號)
rename table user to employee;
insert into employee(id,username,birthday,entry_date,job,salary,resume) values(1,'aaa','2015-09-29','1980-09-09','bbb',90,'aaaaa');
select * from employee;

插入數據的細節1(可以不用指明字段,只要插入的值和表的字段完全匹配就行)
insert into employee values(1,'aaa','1980-09-09','1980-09-09','bbb',90,'aaaaa');

插入數據的細節2(可以把每個插入的字段值都加上單引號,mysql拿到數據以後會自動去轉換成相應的類型)
insert into employee values('1','aaa','1980-09-09','1980-09-09','bbb','90','aaaaa');
插入數據的時候都用單引號引起來,省的數據報錯,如果ID引起來的話,mysql會自動轉換類型的。

插入數據的細節3(插入中文)
    要告訴mysql客戶端採用gb2312編碼
    show variables like 'chara%';
    set character_set_client=gb2312;
    insert into employee(id,username) values('3','張三');
    
    要想查看時不亂碼
    show variables like 'chara%';
    set character_set_results=gb2312;
    select * from employee;

四、更新數據

將所有員工薪水修改爲5000元。
update employee set salary=5000;

將姓名爲’bbb’的員工薪水修改爲3000元。
update employee set salary=3000 where username='bbb';

將姓名爲’bbb的員工薪水修改爲4000元,job改爲ccc。
update employee set salary=4000,job='ccc' where username='bbb';

將bbb的薪水在原有基礎上增加1000元。
update employee set salary=salary+1000 where username='bbb';

更新要注意的問題
update employee set username='ccc',salary=9000,birthday='1980-09-09',.....................
update  where id=1;
這個地方忘記寫where,後果是很嚴重的。

五、刪除表中的記錄

刪除表中名稱爲’zs’的記錄。
delete from employee where username='bbb';

刪除表中所有記錄。
delete from employee;

使用truncate刪除表中記錄。
truncate table employee;

delete 和truncate table的區別
delete是把表中的記錄一條一條地刪除,truncate是摧毀表結構,再重建表結構

六、查詢語句

查詢表中所有學生的信息。
select * from student;

查詢表中所有學生的姓名和對應的英語成績。
select name,english from student;

過濾表中重複的英語數據。
select distinct english from student;

在所有學生總分上加10分特長分。
select name,(chinese+english+math)+10 from student;

統計每個學生的總分。
select name,(chinese+english+math) from student;

使用別名表示學生分數。
select name as 姓名,(chinese+english+math)+10 as 總分 from student;
select name 姓名,(chinese+english+math)+10  總分 from student;

查詢姓名爲王五的學生成績
select * from student where name='王五';

查詢英語成績大於90分的同學
select * from student where english>'90';

查詢總分大於200分的所有同學
select name from student where (chinese+english+math)>200;

查詢英語分數在 80-90之間的同學。
select name from student where english>80 and english<90;
select name from student where english between 80 and 90;  == select name from student where english>=80 and english<=90;

查詢數學分數爲89,90,91的同學。
select * from student where math in(89,90,91);

查詢所有姓李的學生成績。
select * from student where name like '李%';
select * from student where name like '李_';


查詢數學分>80,語文分>80的同學。
select * from student where math>80 and chinese>80;

對數學成績排序後輸出。
select name,math from student order by math; 

對總分排序後輸出,然後再按從高到低的順序輸出
select name 姓名,(chinese+english+math) 總分 from student order by (chinese+english+math) desc;
select name 姓名,(chinese+english+math) 總分 from student order by 總分 desc;

對姓李的學生成績排序輸出
select * from student where name like '李%' order by (chinese+english+math) desc;

統計一個班級共有多少學生?
select count(name) from student;
select count(*) from student;

統計數學成績大於90的學生有多少個?
select count(*) from student where math>80;

統計總分大於250的人數有多少?
select count(*) from student where (chinese+english+math)>250;

關於 count的函數的細節 (count只統計有值的行,當統計列時,如果列值爲空則不統計該列)


統計一個班級數學總成績?
select sum(math) from student;

統計一個班級語文、英語、數學各科的總成績
select sum(chinese),sum(english),sum(math) from student;

統計一個班級語文、英語、數學的成績總和
select sum(chinese+english+math) from student;

統計一個班級語文成績平均分
select sum(chinese)/count(*) from student;

統計一個班級語文成績平均分
select avg(chinese) from student;

求一個班級總分平均分
select avg(chinese+math+english) from student;

求班級最高分和最低分
select max(chinese+math+english),min(chinese+math+english) from student;

對訂單表中商品歸類後,顯示每一類商品的總價
select product,sum(price) from orders group by product;

查詢購買了幾類商品,並且每類總價大於100的商品
select product from orders group by product having sum(price)>100;

where和having的區別
兩者都可用於過濾,但是where後面不能跟合計函數,having可以跟合計函數,一般和group by結合使用

七、定義約束

定義主鍵約束(每一個表必須有一個主鍵列,不允許爲空,必須插入)
create table student
(
    id int  primary key,
    name varchar(40)
);

定義主鍵自動增長(程序員不用管定義爲主鍵自動增長的字段,由數據庫統管理,注意當刪除了一條記錄後再插入一條記錄自動增長的字段會跳過之前出現過的值,因爲已經加到那個值了,會

繼續添加)
create table student
(
    id int  primary key auto_increment,
    name varchar(40)
);

定義唯一約束(該字段的值不能重複,如名字不能重複)
drop table student;
create table student
(
    id int primary key auto_increment,
    name varchar(40) unique
);

定義非空約束(該字段的值不能爲空)
drop table student;
create table student
(
    id int primary key auto_increment,
    name varchar(40) unique not null
);

定義外鍵約束
create table husband
(
    id int primary key,
    name varchar(40)
);

create table wife
(
    id int primary key,
    name varchar(40),
    husband_id int,
    constraint husband_id_FK foreign key(husband_id) references husband(id)
);

八、在實際開發中數據庫表的設計方案

一對多或多對一的對象存到數據庫時,表的設計方案
部門和員工
create table department
(
    id int primary key,
    name varchar(40)
);

create table employee
(
    id int primary key,
    name varchar(40),
    salary decimal(8,2),
    department_id int,
    constraint department_id_FK foreign key(department_id) references department(id)
);


多對多對象的表的設計(老師和學生)
create table teacher
(
    id int primary key,
    name varchar(40),
    salary decimal(8,2)
);

create table student
(
    id int primary key,
    name varchar(40)
);

create table teacher_student
(
    teacher_id int,
    student_id int,
    primary key(teacher_id,student_id),
    constraint teacher_id_FK foreign key(teacher_id) references teacher(id),
    constraint student_id_FK foreign key(student_id) references student(id)    
);
 

一對一的對象的數據庫設計
create table person
(
    id int primary key,
    name varchar(40)
);

create table idcard
(
    id int primary key,
    city varchar(40),
    constraint id_FK foreign key(id) references person(id)    
);


自連接的表
create table person
(
    id int primary key,
    name varchar(40),
    parent_id int,
    constraint parent_id_FK foreign key(parent_id) references person(id)
);

發佈了49 篇原創文章 · 獲贊 59 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章