create table Student(
StudentNo int(4) not null comment '學號',
LoginPwd varchar(20) null comment '',
StudentName varchar(20) null comment '學生姓名',
Ses tinyint(1) null comment '性別,取值0或1',
GradeId int(11) null comment '年級編號',
Phone varchar(50) not null comment '聯繫電話,容許爲空,便可選輸入',
Address varchar(255) not null comment '地址,容許爲空,便可選爲空',
BornDate varchar(50) null comment '出生時間',
Email varchar(50) not null comment '郵箱帳號,容許爲空,便可選輸入',
IdentityCard varchar(18) null comment '身份證號'
);php
create table result(
StudentNo int(4) not null comment'學號',
SubjectNo int(4) not null comment '課程編號',
ExamDate datetime not null comment '考試日期',
StudentResult int(4) not null comment '考試成績'
);java
alter table Student rename as students;
alter table students add Address VARCHAR(256) not null;
alter table students MODIFY Address VARCHAR(256) null;
alter table students CHANGE Address area VARCHAR(256) null;
alter table students drop area;mysql
drop table students;
create table students(
id int(4) primary key auto_increment,
name char(20),
age int(4),
salary double,
denger char(4)
);
insert into students (name) values('張三');
insert into students values (3,'zhangsan',22,800,'男');
insert into students (name,age) values('王三',20);
INSERT into students (name,age) value('lisi',29),('zhaoliu',50);sql
update students set salary=0 where name='張三';
update students set salary=200 where name='張三';
update students set salary=salary+500;
update students set salary=0 where name='王三';
update students set salary=0 where name='lisi';
update students set salary=0 where name='zhaoliu';數據庫
update students set salary =salary+100 where name='lisi' and name='zhaoliu';
update students set salary=salary+500,age=age+10;
update students set age=age+2 where age>=30 and age<50;
DELETE from students where age=34 or age=32;
delete from students;##
TRUNCATE table students;## 刪除數據 不能刪除結構 不記錄日誌 速度快 沒法恢復 會影響自增主鍵的值,從1從新開始,delete 不會從1開始 默認從上次最大值+1開始函數
alter table students modify age not null;命令行
update students set age=30 where name='lisi';
desc students;日誌
select * from students;orm
##dcl命令 grant revoke commit ROLLBACK
##賦權
create user 'admin' IDENTIFIED by 'admin';
show grants for admin;## 查詢用戶擁有哪些權限
grant select,insert,update on 庫名.* to admin;##指定數據庫下全部表的查詢,新增,修改權限排序
grant all PRIVILEGES on *.* to admin; ##給admin全部庫 全部表 的全部權限;
flush PRIVILEGES; ##刷新權限
revoke SELECT,drop on 庫名.* from admin; 回收權限;
##事務操做
## 1 關閉自動提交
set autocommit=0; ##FALSE
## 2開始一個事務,設置一個事務的起點
start TRANSACTION;
## 3xie SQL
## 4 提交或者回滾事務
commit ROLLBACK
## 5設置事務自動提交,恢復默認狀態
set autocommit=1## TRUE 注意 rollback 只能回滾 DML語言 即 insert update delete
##備份
##mysqldump -u 用戶名 -p -c 數據庫名稱 表名1 表名2> 備份文件路徑及文件名
##mysqldumo -u 用戶名 -p -c 數據庫名稱 > 備份文件路徑及文件名
##恢復
## mysql命令行下
source 備份文件路徑及文件名
## cmd 終端模式下
mysql -u root -p java1708 < 文件路徑及文件名
select * from students;
select students.* from students;
select * from students where salary is not null and id between 6 and 8;
select * from students where salary is not null and id in (6,7,8); in ##gu ding zhi
select * from students where salary is not null and id not between 6 and 8;
select * from students where salary is not null and id not in (6,7,8);
select * from students order by age asc; ## asc 升序排序 默認升序
select * from students order by age desc ## jiang xu pai xu
select * from students order by salary desc ,age desc; ## 先排序前面的 後排序後面的
insert into students (name) values ('aaa'),('ccc');
create table score(
student_id int not null;
subject_name varchar(10) not null;
score double,
foreign key(student_id) references students(id)
);
insert into score values (8,'java',90);
insert into score values(9,'java',80);
insert into score values (5,'java',80);
insert into score values (6,'java',80);
insert into score values (10,'java',80);
insert into score values (11,'java',80);
select students.id,students.name,score.subject_name,score.score
from students ,score
where students.id = score.student_id;
select s.id,s.name,c.subject_name,c.score ## as 給表 字段 起別名 as 能夠省略
from s ,c
where s.id = c.student_id;
select students.id,students.name as 姓名,score.subject_name as 科目名稱,score.score as 成績
from students ,score
where students.id = score.student_id;
select * from students;
desc students;
create table information(
id int primary key auto_increment;
message varcharI(64)
):
use java1708;
insert into information(message) values('zhangsan'),('zhangsan2abcdefff'),('zhangsan3afff');
insert into information(message) values('wangsan1abcde'),('lisanabcdefff'),('zhangzhang');
select * from information;
select * from information where message like '%zhangsan%'; 查詢含有zhangsan
select * form information where message like '%f%'; 含有f
select * from information where message like 'wang%'; 以wang 開頭
select * from information where message like '__s%'; 第三個字母是s
select count(*) from information;
select * from information limit 5; 顯示前5條數據
SELECT * FROM information LIMIT 5,5; 顯示第六條到第十條
SELECT * FROM information LIMIT 10,5 顯示第11條 到第 十五條
select student.name,student.age,score.subject_name,score.score
from student,score
where student.id =score.student_id
and student.age between 12 and 45
order bu student.id;
select student.name,student.age,score.subject_name,score.score
from student inner join score
on student.id = score.student_id
and student.age between 12 and 45
order by student.id ;
select student.name,student.id,score.score
from student left join score ##左鏈接 把把左邊的數據所有顯示出來
on student.id =score.student_id
order by student.id;
select student.name,student.id,score.score
from student right join score ##右鏈接 把把右邊的數據所有顯示出來
on student.id =score.student_id
order by student.id;
##建立一個科目表 科目編號 科目名稱 課時
create table subject (
id int primary key auto_increment,
name varchar(16) not null,
classTime int
);
## 修改爲績表結構 學生學號 科目編號 成績
alter table score change subject_name subject_id int;##假如以前的 subject_name有值 須要所有刪除 不然修改不了
select student.name,subject.id,subject.name,score.score
from students join score on student.id=score.student_id
join subject on score.subject_id=subject.id
where score>=60
order by score desc;
select student.name,subject.id,subject.name,score.score
from students left outer join score on student.id=score.student_id
left outer join subject on score.subject_id=subject.id
where score>=60 or score is null
order by score desc;
use MySchool;
##員工表 員工編號 員工姓名 工資 上級領導編號
create table employee(
id int primary key auto_increment,
name varchar(36) not null,
salary double,
pid int
);
insert into employee(name,salary,pid )values('平董事長',10000,0),('張欽佩',9000,1),('胡俊浩',8000,2),('歪哥',8000,2);
## 查詢 員工姓名 員工工資 上級領導名字
select a.name,a.salary,b.name
from employee a,employee b
where a.pid=b.id ;
##查詢歪哥姓名,工資 上級領導名字
select a.name,a.salary,b.name
from employee a,employee b
where a.pid=b.id and a.name='歪哥';
## union 合併數據
select name,salary from emplyoee union select name,salary from students;
## group by
## 根據科目名稱,分別統計每一個科目的最高分 最低分 平均分 max(),min(),avg(),sum(),count()
insert into score select * from score;
select subject.name,max(score),min(score),avg(score )平均分
from subject left outer join score
on subject.id=score.subject_id
where subject.name <> 'php'
group by subject.name ##group by 後跟分組條件
having 平均分>80 ##當 查詢時候有函數的時候 這裏不能用where having 一般用於對統計函數執行結果進行刪選 where後面不能寫統計函數
order by 平均分 desc ; ## order by 後面能夠寫 別名
limit 1; ## 顯示第一條數據
##mysql select a,b,count(*) sum(*) group by
select subject.id '科目編號',subject.name '科目名稱',max(score),min(score),avg(score )平均分
from subject left outer join score
on subject.id=score.subject_id
group by subject.id,subject.name; 除了統計函數以外,最好跟上 查詢上全部的要查詢的字段
##子查詢 統計平均分大於80分的學生信息(姓名 學號 年齡) students subject score
select * from students where
##查詢 java 課程而且分數大於 80 的學生學號
select score.student_id
from score join subject
on score.subject_id=subject.id
where subject.name='java ' and score>80
##子查詢
select score.student_id
from score
where score>80 and score.subject_id in (select id from subject where name=='java')
select * from students where id in (
select score.student_id
from score join subject
on score.subject_id=subject.id
where subject.name='java ' and score>80
);
select * from students
where id in(
select score.student_id
from score
where score>80 and score.subject_id
in(select id from subject where name='java')
);
select count(*) from students; ##count(*) 通常用於統計表中記錄的行數
select count(salary) from students;## count(字段名) 統計該字段非空的記錄總數
select * from students;
##經過建立視圖 展現員工編號 姓名 上級領導編號 視圖中只定義告終構,並不存儲數據,主要用來查詢,數據在表中
create view vm_score as select id,name,pid from employee;
select * from vm_score;
## 經過視圖存儲複雜sql
create view myView01 as select * from students
where id in(
select score.student_id
from score
where score>80 and score.subject_id
in(select id from subject where name='java')
);
select stu_id from (select * from score where c_name ='計算機' and grade>80) a where a.stu_id in(select stu_id from score where grade>80 and c_name='英語')