数据库入门和复习之————————my sql database
2011-08-13 00:39
513 查看
创建数据库:
create database mydb1;
查看数据看:
show databses;
创建一个使用utf-8字符集的mydb2数据库:
create database mydab2 character set utf8;
创建一个使用utf-8字符集并有校队规则的mydb2数据库:
create database mydb3 character set utf8 collate utf8_general_ci;
查看前面创建的mydb2数据库的定义信息:
show create database mydb2;
删除数据库语句:
drop database mydab1;
查看服务器中的数据库,并把其中的某一个库的字符集修改为utf8:
alter database mydb2 charater set gbk;
备份数据库中的数据,并恢复
use test 使用库
show tables; 查看表
select * from user; 查看表中的数据
quit
将数据库中的数据导到另一个文件中
mysqldump -u root -p 1234>d:\test.sql 将数据库中的数据保存到D盘下test的文件下
进入mysql客户端
先删除一个表,再恢复一个表中的数据
use test;使用数据库test
drop table user;删除表user
show tables;查看表
source d:/test.sql;将d盘中test.sql中的数据恢复到数据库中去(source执行sql脚本)
***注意备份和恢复数据库中的表的的前提是数据库没有删除,数据库中的表被删除了,恢复数据库是
恢复数据库中的表中的数据。
创建员工表
use mydb2;
create table employee (
id int,
name varchar(40),
sex varchar(8),
birthday date,
entry_date date,
job varchar(20),
salary double,
resume text
)character set utf8 collate utf8_general_ci;
在上面的员工表的基础上增加一个image列
alter table employee add image blob;
查看表的创建语句
show create table employee;
查看表的结构
desc employee;
修改job列,使其长度增加为60
alter table employee modify job varchar(60);
想退出之前先打单引号,再打;
删除sex列
alter table employee drop sex;
表名改为user
rename table employee to user;
修改表的字符集为gb2312
alter table user character set gb2312;
列名name改为username
alter table user change column name username varchar(100);
=================================================================
sql查询语句
执行数据库脚本文件,将F盘下student.sql这个文件导入到数据库中去
source F:/student.sql;
create table student(
id int,
name varchar(20),
chinese float,
english float,
math float
);
insert into student(id,name,chinese,english,math) values(1,'zhangxiaoming',89,78,90);
insert into student(id,name,chinese,english,math) values(2,'lijin',67,98,56);
insert into student(id,name,chinese,english,math) values(3,'wangwu',87,78,77);
insert into student(id,name,chinese,english,math) values(4,'liyi',88,98,90);
insert into student(id,name,chinese,english,math) values(5,'lilaicai',82,84,67);
insert into student(id,name,chinese,english,math) values(6,'zhangjinbao',55,85,45);
insert into student(id,name,chinese,english,math) values(7,'huangrong',75,65,30);
insert into student(id,name,chinese,english,math) values(8,'章琳',75,76,77);
===================================================================================
1.查询表中所有学生的信息
select * from student;
2.查询表中所有学生的姓名和英语成绩
select name,english from student;
select distinct name,math from student;
3.在所有学生的分数上加上10分特长分
select name,chinese+10,english+10,math+10 From student;
4.统计每个学生的总分
select name,(math+chinese+english) as sum from student;
====================================================================================
使用where字句,进行过滤查询,练习
select * from student where name='lijin';
查询英语成绩大于90分的同学
select name,english from student where english>90;
查询总分大于200分的所有同学
select * from student where (math+english+math)>200;
查询英语分数在 80-90之间的同学。
select * from student where english between 80 and 90;
查询数学分数为89,90,91的同学。
select * from student where math in(89,90,91);
===================================
set character_set_results=gb2312;
=====================================
查询所有姓李的学生成绩。
select * from student where name like 'li%';
select * from student where name like 'li__';
select * from student where name like 'li___';
查询数学分>80,语文分>80的同学。
select * from student where math>80 and chinese>70;
===================================================
SELECT column1, column2. column3..
FROM table;
order by column asc|desc
Order by 指定排序的列,排序的列即可是表中的列名,也可以是select 语句后指定的列名。
Asc 升序、Desc 降序
ORDER BY 子句应位于SELECT语句的结尾。
练习:
对数学成绩排序后输出。
select name,math from student order by math desc;
对总分排序后输出,然后再按从高到低的顺序输出
select name,(chinese+english+math) from student order by (chinese+english+math) desc;
对姓李的学生成绩排序输出
select * from student where name like 'li%' order by (chinese+english+math);
======================================================
count函数 统计一个班一共有多少个学生
select count(*) from student;
统计数学成绩大于90的学生有多少个?
select count(*) from student where math>=90;
统计总分大于250的人数有多少?
select count(*) from student where (chinese+math+english)>250;
=============================
SUM:求和函数
统计一个班级数学总成绩?
select sum(math) from student;
统计一个班级语文、英语、数学各科的总成绩
select sum(chinese),sum(math),sum(english) from student;
统计一个班级语文、英语、数学的成绩总和
select sum(chinese+math+english) from student;
统计一个班级语文成绩平均分
select sum(chinese)/count(chinese) from student;
==================================================
合计函数AVG
求一个班级数学平均分?
select avg(math) from student;
求一个班级总分平均分
select avg(math+chinese+english) from student;
===================================================
合计函数-MAX/MIN
Max/min函数返回满足where条件的一列的最大/最小值
求班级最高分和最低分
select max(math+chinese+english),min(math+chinese+english) from student;
====================================================
将F盘下的testSql/Test.sql文件导到现在的数据库test1中
source F:/testSql/Test.sql;
使用group by 子句对列进行分组
查询订单表中有几样商品
select * from orders group by(name);
======================================================
对订单表中的商品归类后,显示每一种商品花了多少钱
select name,sum(price) from orders group by(name);
========================================================
使用having 子句过滤
Having和where均可实现过滤,但在having可以使用合计函数,
having通常跟在group by后,它作用于组。
查询购买了几类商品,并且每类总价大于100的商品
select name,sum(price) from orders group by(name) having sum(price)>=100;(v)
下面是一个错误的案例,where子句后面不能使用合计函数
select name,sum(price) from orders group by(name) where sum(price)>=100;(x)
========================================================
对日期操作的相关函数
select addtime('02:30:30','01:01:01');
获得当前的时间
select current_date();
获得当前的时间戳
select CURRENT_TIMESTAMP();
select NOW();
=========================================================
对字符串操作的相关函数
将两个字符串连接起来
select concat('aaa','bbb');
去掉两边空格的两个函数
select trim(' aabbccdd ');
=========================================================
定义表的约束
创建带主键约束的表
create table test1
(
id int primary key,
name varchar(20)
);
主键约束表示主键既不能为空,也不能重复
insert into test1(id,name) values(1,'hw');
mysql> insert into test1(id,name) values(null,'js');
================================
测试唯一性
create table test2
(
name varchar(20) unique
)
=================================
定义主键自动增长,用户没必要来维护这个主键了,由数据库自己来维护这个主键的增长了
create table test3
(
id int primary key auto_increment,
name varchar(20)
);
======================
定义外键约束
数据库设计的几个原则
一个实体就对应一张表
对象上有几个基本属性,就在表中有几个基本字段
在多的一方加外键
数据库的约束一定要加的严格,宁肯错杀一千,不可错杀一个,不要让一个坏数据进来
设计一个部门表
create table department
(
id int primary key auto_increment,
name varchar(20) not null
);
设计一个员工表,为其增加外键约束
create table employee
(
id int primary key auto_increment,
name varchar(20) not null,
department_id int,
constraint department_id_FK foreign key(department_id) references department(id)
);
===================================================
表的关系之____多对多的关系
create table teacher
(
id int primary key auto_increment,
name varchar(20) not null
);
create table student
(
id int primary key auto_increment,
name varchar(20) not null,
phone varchar(20)
);
中间关系表,使用联合主键标识唯一的一行,指名外键约束
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 auto_increment,
name varchar(20)
)
再创建一个身份证的表,这个表的ID既作为主键,又作为外键
create table idcard
(
id int primary key,
constraint idcard_id_FK foreign key(id) references person(id)
)
create database mydb1;
查看数据看:
show databses;
创建一个使用utf-8字符集的mydb2数据库:
create database mydab2 character set utf8;
创建一个使用utf-8字符集并有校队规则的mydb2数据库:
create database mydb3 character set utf8 collate utf8_general_ci;
查看前面创建的mydb2数据库的定义信息:
show create database mydb2;
删除数据库语句:
drop database mydab1;
查看服务器中的数据库,并把其中的某一个库的字符集修改为utf8:
alter database mydb2 charater set gbk;
备份数据库中的数据,并恢复
use test 使用库
show tables; 查看表
select * from user; 查看表中的数据
quit
将数据库中的数据导到另一个文件中
mysqldump -u root -p 1234>d:\test.sql 将数据库中的数据保存到D盘下test的文件下
进入mysql客户端
先删除一个表,再恢复一个表中的数据
use test;使用数据库test
drop table user;删除表user
show tables;查看表
source d:/test.sql;将d盘中test.sql中的数据恢复到数据库中去(source执行sql脚本)
***注意备份和恢复数据库中的表的的前提是数据库没有删除,数据库中的表被删除了,恢复数据库是
恢复数据库中的表中的数据。
创建员工表
use mydb2;
create table employee (
id int,
name varchar(40),
sex varchar(8),
birthday date,
entry_date date,
job varchar(20),
salary double,
resume text
)character set utf8 collate utf8_general_ci;
在上面的员工表的基础上增加一个image列
alter table employee add image blob;
查看表的创建语句
show create table employee;
查看表的结构
desc employee;
修改job列,使其长度增加为60
alter table employee modify job varchar(60);
想退出之前先打单引号,再打;
删除sex列
alter table employee drop sex;
表名改为user
rename table employee to user;
修改表的字符集为gb2312
alter table user character set gb2312;
列名name改为username
alter table user change column name username varchar(100);
=================================================================
sql查询语句
执行数据库脚本文件,将F盘下student.sql这个文件导入到数据库中去
source F:/student.sql;
create table student(
id int,
name varchar(20),
chinese float,
english float,
math float
);
insert into student(id,name,chinese,english,math) values(1,'zhangxiaoming',89,78,90);
insert into student(id,name,chinese,english,math) values(2,'lijin',67,98,56);
insert into student(id,name,chinese,english,math) values(3,'wangwu',87,78,77);
insert into student(id,name,chinese,english,math) values(4,'liyi',88,98,90);
insert into student(id,name,chinese,english,math) values(5,'lilaicai',82,84,67);
insert into student(id,name,chinese,english,math) values(6,'zhangjinbao',55,85,45);
insert into student(id,name,chinese,english,math) values(7,'huangrong',75,65,30);
insert into student(id,name,chinese,english,math) values(8,'章琳',75,76,77);
===================================================================================
1.查询表中所有学生的信息
select * from student;
2.查询表中所有学生的姓名和英语成绩
select name,english from student;
select distinct name,math from student;
3.在所有学生的分数上加上10分特长分
select name,chinese+10,english+10,math+10 From student;
4.统计每个学生的总分
select name,(math+chinese+english) as sum from student;
====================================================================================
使用where字句,进行过滤查询,练习
select * from student where name='lijin';
查询英语成绩大于90分的同学
select name,english from student where english>90;
查询总分大于200分的所有同学
select * from student where (math+english+math)>200;
查询英语分数在 80-90之间的同学。
select * from student where english between 80 and 90;
查询数学分数为89,90,91的同学。
select * from student where math in(89,90,91);
===================================
set character_set_results=gb2312;
=====================================
查询所有姓李的学生成绩。
select * from student where name like 'li%';
select * from student where name like 'li__';
select * from student where name like 'li___';
查询数学分>80,语文分>80的同学。
select * from student where math>80 and chinese>70;
===================================================
SELECT column1, column2. column3..
FROM table;
order by column asc|desc
Order by 指定排序的列,排序的列即可是表中的列名,也可以是select 语句后指定的列名。
Asc 升序、Desc 降序
ORDER BY 子句应位于SELECT语句的结尾。
练习:
对数学成绩排序后输出。
select name,math from student order by math desc;
对总分排序后输出,然后再按从高到低的顺序输出
select name,(chinese+english+math) from student order by (chinese+english+math) desc;
对姓李的学生成绩排序输出
select * from student where name like 'li%' order by (chinese+english+math);
======================================================
count函数 统计一个班一共有多少个学生
select count(*) from student;
统计数学成绩大于90的学生有多少个?
select count(*) from student where math>=90;
统计总分大于250的人数有多少?
select count(*) from student where (chinese+math+english)>250;
=============================
SUM:求和函数
统计一个班级数学总成绩?
select sum(math) from student;
统计一个班级语文、英语、数学各科的总成绩
select sum(chinese),sum(math),sum(english) from student;
统计一个班级语文、英语、数学的成绩总和
select sum(chinese+math+english) from student;
统计一个班级语文成绩平均分
select sum(chinese)/count(chinese) from student;
==================================================
合计函数AVG
求一个班级数学平均分?
select avg(math) from student;
求一个班级总分平均分
select avg(math+chinese+english) from student;
===================================================
合计函数-MAX/MIN
Max/min函数返回满足where条件的一列的最大/最小值
求班级最高分和最低分
select max(math+chinese+english),min(math+chinese+english) from student;
====================================================
将F盘下的testSql/Test.sql文件导到现在的数据库test1中
source F:/testSql/Test.sql;
使用group by 子句对列进行分组
查询订单表中有几样商品
select * from orders group by(name);
======================================================
对订单表中的商品归类后,显示每一种商品花了多少钱
select name,sum(price) from orders group by(name);
========================================================
使用having 子句过滤
Having和where均可实现过滤,但在having可以使用合计函数,
having通常跟在group by后,它作用于组。
查询购买了几类商品,并且每类总价大于100的商品
select name,sum(price) from orders group by(name) having sum(price)>=100;(v)
下面是一个错误的案例,where子句后面不能使用合计函数
select name,sum(price) from orders group by(name) where sum(price)>=100;(x)
========================================================
对日期操作的相关函数
select addtime('02:30:30','01:01:01');
获得当前的时间
select current_date();
获得当前的时间戳
select CURRENT_TIMESTAMP();
select NOW();
=========================================================
对字符串操作的相关函数
将两个字符串连接起来
select concat('aaa','bbb');
去掉两边空格的两个函数
select trim(' aabbccdd ');
=========================================================
定义表的约束
创建带主键约束的表
create table test1
(
id int primary key,
name varchar(20)
);
主键约束表示主键既不能为空,也不能重复
insert into test1(id,name) values(1,'hw');
mysql> insert into test1(id,name) values(null,'js');
================================
测试唯一性
create table test2
(
name varchar(20) unique
)
=================================
定义主键自动增长,用户没必要来维护这个主键了,由数据库自己来维护这个主键的增长了
create table test3
(
id int primary key auto_increment,
name varchar(20)
);
======================
定义外键约束
数据库设计的几个原则
一个实体就对应一张表
对象上有几个基本属性,就在表中有几个基本字段
在多的一方加外键
数据库的约束一定要加的严格,宁肯错杀一千,不可错杀一个,不要让一个坏数据进来
设计一个部门表
create table department
(
id int primary key auto_increment,
name varchar(20) not null
);
设计一个员工表,为其增加外键约束
create table employee
(
id int primary key auto_increment,
name varchar(20) not null,
department_id int,
constraint department_id_FK foreign key(department_id) references department(id)
);
===================================================
表的关系之____多对多的关系
create table teacher
(
id int primary key auto_increment,
name varchar(20) not null
);
create table student
(
id int primary key auto_increment,
name varchar(20) not null,
phone varchar(20)
);
中间关系表,使用联合主键标识唯一的一行,指名外键约束
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 auto_increment,
name varchar(20)
)
再创建一个身份证的表,这个表的ID既作为主键,又作为外键
create table idcard
(
id int primary key,
constraint idcard_id_FK foreign key(id) references person(id)
)
相关文章推荐
- SQL入门(1)——创建,删除,查看数据库(database)信息
- 数据库复习总结(6)-SQL语句入门(脚本、命令)
- 数据库连接问题之:Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
- ASP编程入门进阶(廿一):DAO SQL之建立数据库表
- T-SQL复习总结--用T-SQL创建,修改,管理,删除数据库
- SQL 2005 建立数据库图表时出现以下错误Database diagram support objects cannot be installed because this database does not have a valid owner 解决
- html 5 本地数据库(Web Sql Database)核心方法
- html 5 本地数据库(Web Sql Database)核心方法openDatabase、transaction、executeSql 详解
- mysql:sql create database新建utf8mb4 数据库
- html 5 本地数据库(二)-- Web Sql Database核心方法openDatabase、transaction、executeSql 详解
- T-SQL入門攻略之10-修改数据库alter database
- MDT 2013 从入门到精通之SQL Configure Database Rules
- ScottGu之博客翻译-LINQ to SQL第四部分,更新数据库 LINQ to SQL (Part 4 - Updating our Database)
- 数据库入门之运行原始 SQL 查找x
- html5 本地数据库-Web SQL Database操作类
- sql语法复习:增删查改,各种数据库对象创建和函数使用
- [SQL Database] 内外网数据库同步
- Azure SQL 数据库仓库Data Warehouse (1) 入门
- SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件
- SQL SERVER 2008配置Database Mail –用SQL 数据库发邮件