DDL (数据定义问题)
数据定义语言 - Data Definition Language
用来定义数据库的对象,如数据表、视图、索引等
DML (数据操纵问题)
数据处理语言 - Data Manipulation Language
在数据库表中更新,增加和删除记录
如 update, insert, delete
DCL (数据控制问题)
数据控制语言 – Data Control Language
指用于设置用户权限和控制事务语句
如grant,revoke,if…else,while,begin transaction
DQL (数据查询问题)
数据查询语言 – Data Query Language
select
=====================================================
登陆:
mysql (-h 连接的主机ip -P端口3306)-u 用户名 -p 密码---( 连接本机: 省略 -h 和 -P 主机和端口。)
查询当前的所有库
show databases;
查询当前数据的创建方式:查看数据的编码表
show create database 库名;
创建库
create database 数据库名 由于创建数据库时没有指定编码表,因此会使用安装数据库时默认的编码表
create database 数据库名 character set 编码表名; 创建数据库会使用指定的编码表
create database 数据库名 character set 编码表名 collate 排序规则; 使用指定的编码表同时还可以根据编码表指定排序规则
删除数据库
drop database 数据库名
修改数据库编码集
alter database 数据库名称 character set collate 比较规则
切换数据库和查看正在使用的数据
use 数据库名
show database();
============================================
创建数据表
create table 表名(列名 类型(长度) 约束)
create table employee2(
id int primary key auto_increment, 当前这一列是主键
name varchar(32) not null , //不能为null
sex varchar(10),
password varchar(32) unique not null,
birthday date
);
查看创建的表的结构
desc 表名
数据表结构的修改
alter table 表名 增/删/改 列名 类型(长度)约束
1、增加列:
alter table 表名 add 列名 类型(长度) 约束
2、修改现有的列:
alter table 表名 modify 列名 类型(长度)约束
3、修改现有列的名称
alter table 表名 change 旧列名 新列名 类型(长度)约束
4、删除现有的列
alter table 表名 drop 列名
5、修改表名
rename table 旧列名 to 新列名
6、修改表的字符集
alter table 表名 character set utf8;
数据表的删除
drop table 表名
查看当前库中有多少表
show tables;
查看表的结构
desc table 表名
查看表的编码
show create table 表名
==================================================
数据表的增删改查
向数据表中插入数据
insert into 表名(列名1,...)values (值1,。。);(当值全都要写时,列名可以省略)
修改数据记录
update 表名 set 列名=值,。。。。where 条件语句
注意:一般在修改数据表中的数据时,都需要加条件,而不能直接修改表中的某一列的所有值。
update person set username=’王五’,password='123' where id =3;
删除数据表记录
delete from 表名 where 条件语句
删除所有
delete from person;
查询数据表中的所有记录
select * from 表名
查询指定列记录
select 列名,列名... form 表名 where条件语句
========运算符=====================================
相等= 不等
2) 区间:between ...and... 在两者之间取值 between 70 and 80
等价于 >=70 =24 and sage 磁盘SQL文件路径
mysqldump -u root -p mydb3 > c:\mydb3.sql
由于mysqldump命令不是sql命令,需要在dos窗口下使用。
恢复数据,需要手动的先创建数据库:
create database mybd3;
mysql -u root -p mybd3 any(1,2,3) 等价于 min(1,2,3)
all 所有数据 >all(1,2,3) 等价于 max(1,2,3)
1、查询最高分:
studentcource 找到最高分
select max(score) fromstudentcource ;
2、需要从studentcource 表中查出 最高分对应的学生id
select student_id from studentcource where score = ( select max(score) fromstudentcource );
select student_id from studentcource where score > all( select score from studentcource );
3、根据学生的id到student表中找出学生信息
select * from student where id in( select student_id from studentcource where score = ( select max(score) from studentcource ) );
select * from student where id in( select student_id from studentcource where score >= all( select score from studentcource ));
查询编号2课程比编号1课程成绩高所有学生信息:
1、编号2 和编号1 的课程成绩:
select score from studentcource where cource_id = 1 or cource_id = 2;
2、查找出编号2 比 编号1 成绩高的学生id
找出编号1的成绩的最高分
select score from student where cource_id = 1;
使用子查询的结果,我们可以把它当做一个临时表存在,然后在从其中找出需要的结果:
select max(temp.score) from (select score from studentcource where cource_id = 1) as temp;
找出编号2 的成绩高于 编号 1 的 学生id
select student_id from studentcource where cource_id = 2 and score > ( select max(score) from studentcource where cource_id = 1 );
select student_id from studentcource where cource_id = 2 and score > all( select score from studentcource where cource_id = 1 );
select * from student where id in ( select student_id from studentcource where cource_id = 2 and score > all( select score from studentcource where cource_id = 1 ) );
额外附加:
思考,需要查询编号2课程比编号1课程成绩高所有学生信息 ,同时列出学生的成绩。
如果数据来自多张表,这时需要把不同的表进行连接查询。
思路:需要把student 表 和 studentcource 表中的 成绩现实。
select * from student ,( select score ,student_id from studentcource where cource_id = 2 and score > all( select score from studentcource where cource_id = 1 ) )as temp where student.id = temp.student_id ;
2.5、mysql的自带函数
msyql数据库中自带了一些函数。在我们使用sql语句操作mysql数据库的时候,可以直接使用这些函数。
===========一对多的表设计================================================================
=================多对多之表的设计原则=====================================
1、在任意一方添加外键都失败,要设计中间表。
2、创建顺序,先创建“父表”。
3、创建中间表
4、外键约束
5、采用联合主键(一组值不能出现两次)。
=======================================================
一般是在先创建完父表之后再建立表之间的关系。如下:
create table if not exists orders(
id int primary key auto_increment,
name varchar(20),
info varchar(100)
);
create table if not exists products(
id int primary key auto_increment,
name varchar(20),
price double(10.2)
);
create table if not exists orders_products(
oid int, --订单主键
pid int,---商品主键
buynum int --一张订单商品购买数量
primary key(oid,pid)--联合主键
);
--添加主键
====================================================================================
alter table orders_products add constraint orders_products_orders_fk foreign key(oid) reference orders(id);
alter table orders_products add constraint orders_products_products_fk foreign key(pid) reference products(id);
/*
还有一种就是在见表的的时候就建立关系了:
如在多方(直接在最后书写):
constraint user_orders_fk foreign key(uid) reference user(id);
*/
===================================================
在一对多的情况下,删除表的时候,先删除多的一方,再,删除一方,否则删除了表。
=========================================================================
=======================================================