创建数据库:
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)
)

数据库入门和复习之————————my sql database相关推荐

  1. 数据库入门必读书籍推荐《Beginning Database Design》和《《Beginning SQL Queries》

    数据库入门必读书籍推荐<Beginning Database Design>和<<Beginning SQL Queries> <Beginning Databas ...

  2. azure不支持哪些语句 sql_排查 Azure SQL 数据库的常见连接问题 - Azure SQL Database | Microsoft Docs...

    您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn. 排查 Azure SQ ...

  3. azure 入门_Azure SQL数据库入门

    azure 入门 In this article, we will uncover the basics of the Azure SQL Database. 在本文中,我们将揭示Azure SQL数 ...

  4. aws 数据库迁移_将您的sql数据库迁移到云AWS和Azure

    aws 数据库迁移 When migrating your SQL databases to the cloud you need to make your choice carefully. Mig ...

  5. sql创建计算机用户,2015年计算机四级数据库复习要点:SQL Server 登录账户

    2015年计算机四级数据库复习要点:SQL Server 登录账户 系统内置的登录账户 1. BUILTIN\Administrators:是一个Windows组账户,表示所有的Windows Adm ...

  6. php查询sql2008数据库操作系统,使用 PHP 进行查询 - Azure SQL Database SQL Managed Instance | Microsoft Docs...

    您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn. 快速入门:使用 PHP ...

  7. sql datetime 加一天_PowerQuery数据库Sql.Database条件查询

    焦棚子的文章目录 请点击下载附件 1.应用场景 底层数据在数据库(sql server数据库,其他数据库同理,下文不再说明.)中,Excel中有查询的字段,需要在数据库中查询相关信息: 2.举个栗子 ...

  8. SQL数据库入门基础

      SQL(Structure Query Language,结构化查询语言)语言是国际标准化组织(ISO)采纳的标准数据库语言. 数据库就是一幢大楼,我们要先盖楼,然后再招住户(住户当然就是数据库对 ...

  9. html 5 本地数据库(Web Sql Database)

    基于HTML5的Web DataBase 可以让你在浏览器中进行数据持久地存储管理和有效查询,假设你的离线应用程序有需要规范化的存储功能 本文讲述如何使用核心方法openDatabase.transa ...

最新文章

  1. UNITY调用安桌方法出现 JNI: Init'd AndroidJavaClass with null ptr!
  2. 直面故障,我们该怎么做?
  3. 科大星云诗社动态20201121
  4. CFA将人工智能列入考试内容折射啥
  5. 技校毕业是什么学历_技校毕业了是什么学历
  6. 第二十三天 how can I 坚持
  7. PythonEggs
  8. 如何以用户身份登录MySQL_解析:如何以普通用户的身份运行 MySQL
  9. tomcat 7 无法打开管理页面
  10. 第三章 文件IO复习
  11. ASP.NET 的服务器端控件有三种关于 ID 的属性 ID, ClientID 和 UniqueID
  12. 火狐扩展教程_4个值得一试的Firefox扩展
  13. JS new一个对象的过程
  14. java阶段测试A卷含答案
  15. 太上老君的炼丹炉之分布式 Quorum NWR
  16. 写一个PE的壳_Part 3:Section里实现PE装载器
  17. Ubuntu下安装PCL1.12.1点云库经验分享
  18. in unnamed module of loader
  19. 【PSO三维路径规划】粒子群算法融合鸡群算法多无人机三维路径规划【含Matlab源码 1792期】
  20. 阿里短信服务 JAVA

热门文章

  1. 《明日方舟》游戏拆解
  2. 说说ShellExecuteEx
  3. 什么浏览器好用啊(浏览器排行榜前十名)
  4. MIDI 转3.5mm音频接口连接方式
  5. 新一代 PHP 框架 QeePHP 发布 - 主要特征
  6. PNAS | 物种入侵改变了二十年淡水时间序列的微生物群落物候特征
  7. 苹果ceo乔布斯_苹果CEO乔布斯因胰腺癌去世,医生提醒,胰腺不好有四个信号
  8. chmod 赋权所有_chmod 权限 命令详细用法
  9. 《京韵大鼓——击鼓骂曹》(骆玉笙)(唱词文本)
  10. DTC的解析与状态掩码