Structured Query Language
的缩写(结构化查询语言)是一种定义、操做、管理关系数据库的句法结构化查询语言的工业标准由ANSI
(美国国家标准学会,ISO的成员之一)维护。java
优势:最大的优势共享数据、安全性有很大的保障,可是也不是绝对的安全(黑客)、操做数据很容易mysql
## 二、经常使用数据库sql
Oracle DB2 Informix Sybase SQL Server ProstgreSQL面向对象数据库 MySQL Access SQLite等数据库
alter database mydb1 character set utf8
;create database mydb1 character set gbk
;show collation like '%gb%'
;gbk_bin
create database mydb2 character set gbk collate gbk_bin
;create table t( id int , name varchar(30) ) ;
show create table t ;
create table t4 ( id int , name varchar(30), optime timestamp ) ;
set character_set_client=gbk;
set character_set_results=gbk ; insert into t4(id,name) values(1,'张无忌') ; insert t4(id,name) values(2,'乔峰') ;
into 能够省略insert t4 values(3,'杨过','2014-4-3') ;
update t4 set name='杨康' where id = 3 ;
update t4 set name = '东方不败' ;
update t4 set id=6,name='萧峰' where id = 2 ;
delete from t4 where id = 4 ;
delete from t4 ;
truncate table t4 ;
alter table t4 add address varchar(100) ;
alter table t4 drop column address ;
desc t4 ;
建立一个学生表安全
create table stu ( id int primary key, // 主键约束 name varchar(30) unique, // 惟一约束 sex char(2) not null, //非空约束 age int check (age > 0 and age < 100),// 检查约束 address varchar(50) default '北京' //默认约束 ) ;
bash
insert into stu values(1,'张无忌','男',20,'北京') ; insert into stu values(2,'小龙女','女',18,'古墓') ; insert into stu values(3,'黄蓉','女',15,'桃花岛') ; insert into stu values(4,'韦小宝','男',24,'扬州') ; insert into stu values(5,'乔峰','男',34,'雁门关') ; insert into stu values(6,'张果老','男',30,'雁门关') ; insert into stu values(7,'老张','男',38,'黒木崖') ; insert into stu values(8,'张','男',34,'桃花岛') ; insert into stu values(9,'韦小宝','女',24,'新东方') ; insert into stu(id,name,sex,age) values(10,'令狐冲','男',27) ;
服务器
查看全部数据 select * from stu ;
函数
查看小龙女的信息 select * from stu where id = 2 ; select * from stu where name='小龙女' ;ui
查看年龄在20~30之间的人 select * from stu where age >=20 and age <=30 ; select * from stu where age between 20 and 30 ; # 包括20和30 #查看全部的的姓名 select name from stu ;spa
查看全部的的姓名,年龄,性别 select name,age,sex from stu ;
select * from stu where name like '张__' or name like '_张_' or name like '__张' ;
select distinct sex from stu ;
select distinct name,sex from stu ;
create table score ( id int primary key, sid int , china int, english int , history int, constraint sid_FK foreign key(sid) references stu(id) ) ;
insert into score values(1,1,68,54,81) ;
insert into score values(2,3,89,98,90) ;
insert into score values(3,4,25,60,38) ;
insert into score values(4,6,70,75,59) ;
insert into score values(5,8,60,65,80) ;
复制代码
select id,china+10,english,history from score ;
select id as 编号,china as 语文,english as 英语,history as 历史 from score ; select id 编号,china 语文,english 英语,history 历史 from score ;
select id,china + english + history 总分 from score ;
select * from stu where address = '桃花岛' or address = '黒木崖' ;
select * from stu where address in('桃花岛','黒木崖') ;
select * from stu where address is null ;
select * from stu where address is not null ;
对考试的人的语文升序asc
排列 select * from score order by china asc;
对考试的人的历史降序desc
排列 select * from score order by history desc;
根据多个字段进行排序(语文升序,对语文成绩同样的人再进行历史降序类排)select * from score order by china asc,history desc;
根据考试总分降序进行排序 select *,china + english + history 总分 from score order by china + english + history desc ;
主键:惟一的去区分每一条记录的一列或者多列的值. 特色:惟一,非空 Order by
指定排序的列,排序的列便可是表中的列名,也能够是select 语句后指定的列名。 Asc
升序、Desc
降序 ORDER BY
子句应位于SELECT语句的结尾。
alter table score add constraint stu_score_FK foreign key(sid) references stu(id) ;
alter table score drop foreign key stu_score_FK ;
多表查询
交叉查询
查询每一个人的考试成绩 select * from stu s cross join score c on s.id = c.sid
;
查询参加考试的人的成绩 select name,china,english,history,china+english+history 总分 from stu s inner join score c on s.id = c.sid ;
查询全部人的成绩 select name,china,english,history,china+english+history 总分 from stu s left out join score c on s.id = c.sid ;
查询没有参加考试的人select * from stu where id not in(select sid from score) ;
查询参加考试的人的成绩select name,china,english,history,china+english+history 总分 from stu s,score c where s.id = c.sid ;
聚合函数 sum max,min avg ,count
分组函数select count(*) 数量,sex,name from stu group by sex,name ;
根据多个字段进行分组
分组条件select count(*),sex from stu where age >=16 group by sex having count(*) >1 ;
运算符 Like语句中,% 表明零个或多个任意字符,_ 表明一个字符,例first_name like ‘_a%’
primary key
惟一约束unique
检查约束 MySQL不支持哦int check (age > 0 and age < 100)
非空约束not null
create table stu ( id int primary key, // 主键约束 name varchar(30) unique, // 惟一约束 sex char(2) not null, //非空约束 age int check (age > 0 and age < 100),// 检查约束 address varchar(50) default '北京' //默认约束 ) ;
链接查询
子查询
SELECT * FROM orders WHERE customer_id=(SELECT id FROM customer WHERE name LIKE '%陈冠希%');
联合查询
报表查询
数值类型
BIT(M) 位类型。M指定位数,默认值1,范围1-64
TINYINT [UNSIGNED] [ZEROFILL] 带符号的范围是-128到127。无符号0到255。
BOOL,BOOLEAN 使用0或1表示真或假
SMALLINT [UNSIGNED] [ZEROFILL] 2的16次方
INT [UNSIGNED] [ZEROFILL] 2的32次方
BIGINT [UNSIGNED] [ZEROFILL] 2的64次方
FLOAT[(M,D)] [UNSIGNED] [ZEROFILL] M指定显示长度,d指定小数位数
DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] 表示比float精度更大的小数
文本、二进制类型
时间日期
时间日期相关函数
select addtime(‘02:30:30’,‘01:01:01’);
注意:字符串、时间日期的引号问题 select date_add(entry_date,INTERVAL 2 year) from student;
//增长两年 select addtime(time,‘1 1-1 10:09:09’) from student; 
//时间戳上增长,注意年后没有 字符串相关函数
数学相关函数
数据库备份: mysqldump -u root -psorry test>D:\test.sql
数据库恢复:
mysql -u root -psorry test<test.sql