DDL:数据库定义语句
  
DML:增删改查
     
DCL:数据库控制语句
     事物
数据库的表设计:
     


一、库操作
1.创建数据库
create database mydb1;
show databases;
2.创建带字符集的数据库
create database mydb2 CHARACTER SET=utf8;
show create database mydb2;
3.创建带校验的数据库
create database mydb3 CHARACTER SET=utf8 COLLATE utf8_general_ci;   
4.显示数据库语句:SHOW DATABASES
5.显示数据库创建语句(详情) SHOW C
REATE DATABASE mydb3 
6.数据库删除语句:
DROP DATABASE  mydb3;


7.修改数据库的库字符编码
ALTER  DATABASE  mydb2 character set gb2312;
8.备份test库中的数据,并恢复(备份有一种方式,恢复有两种方式)
mysqldump -u root -p mydb2>c://test.sql 是一个win命令
 第一种方式:
       1.创建库
          create database mydb2;
          use mydb2;
       2.恢复数据
 source c://test.sql;
 第二种方式:win命令
       1.创建库
          create database mydb2;
          use mydb2;
 exit;
       2.恢复数据
  mysql -u root -p mydb2<c://test.sql  
9.创建表
1)create table test
(
   name varchar(40)
);
2)增加数据
insert into test values("aaa");




二、表操作
1.创建数据库表
 create table employee
(
    id int,
    name varchar(40),
    sex  char(4),
    birthday date,
    Entry_date date,
    job  varchar(100),
    salary  Decimal(8,2),
    resume  Text
);




2.在上面员工表的基本上增加一个image列。
alter table employee add image blob;3.修改job列,使其长度为60。
alter table employee modify job varchar(60);


4.删除sex列。
alter table employee drop image;


5.表名改为user。
rename table employee to user;


6.修改表的字符集为utf-8alter table user character set gbk;
alter table user character set utf8;
7.列名name修改为username
删除表
alter table user change column name username varchar(100);
8.删除表
drop table user;


三、增删改查
准备表
create table employee
(
id int,
name varchar(40),
sex varchar(4),
birthday date,
entry_date date,
salary decimal(8,2),
resume text
);


1.插入数据
insert into employee(id,name,sex,birthday,entry_date,salary,resume) values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer');
//可以省略表字段,但是必须插入全部字段
insert into employee values(null,null,'male','1993-03-04','2016-11-10','1000','i am a developer');
//无法插入
insert into employee values('male','1993-03-04','2016-11-10','1000','i am a developer');
//可以包起来所有类型
insert into employee values('5',null,'male','1993-03-04','2016-11-10','1000','i am a developer');
//指定某些列插入数据
insert into employee(id) values(6);


//插入汉字
insert into employee(id,name) values(6,'张三');


要告诉mysql客户采用gb2312编码
show variables like 'chara%';
set character_set_client=gb2312;
insert into employee(id,username) values('3','张三');

要想查看时不乱码
show variables like 'chara%';
set character_set_results=gb2312;
select * from employee;




2.修改表数据
将所有员工薪水修改为5000元。
update employee set salary=5000;


将姓名为’zs’的员工薪水修改为3000元。
update employee set salary = 3000 where name='zhangsan';


将姓名为’aaa’的员工薪水修改为4000元,job改为ccc。
update employee set salary = 4000,job='ccc' where name='张三';


将wu的薪水在原有基础上增加1000元。
update employee set salary = salary+1000 where name='张三';


3.删除
删除表中名称为’zs’的记录。
delete from employee where job='ccc';


删除表中所有记录。
delete from employee;


使用truncate删除表中记录
truncate table employee;
//细节
delete from employee where  


4.查询


查询表中所有学生的信息。
select id,name,chinese,english,math from student;


查询表中所有学生的姓名和对应的英语成绩。
select name,english from student;


过滤表中重复数据。select distinct english from student;


在所有学生分数上加10分特长分。
select name,(chinese+english+math)+10 from  student;


统计每个学生的总分。
select name,(chinese+english+math) from  student;


使用别名表示学生分数
select name,(chinese+english+math) as 总分 from  student;
//可以不用as
select name,(chinese+english+math) 总分 from  student;


查询姓名为wu的学生成绩
select * from student where name='张三';


查询英语成绩大于90分的同学
select * from student where  english>'90';


查询总分大于200分的所有同学
select name,(chinese+english+math) 总分 from  student where chinese+english+math>200;




查询英语分数在 80-90之间的同学。
select * from student where english>=80 and english=<90;


select * from student where english between 80 and 90;


查询数学分数为89,90,91的同学。
select * from student where math=89 or math=90 or math=91;
select * from student where math in(89,90,91);


查询所有姓李的学生成绩。
select * from student where name like '李%';
select * from student where name like '李_';


查询数学分>80,语文分>80的同学
select * from student where math>80 and chinese>80;
select * from student where chinese is null;




对数学成绩排序后输出。
select name,math from student order  by math desc;对总分排序后输出,然后再按从高到低的顺序输出
select name,math+english+chinese from student order  by math+english+chinese desc;


对姓李的学生成绩排序输出
select name,math+english+chinese from student where name like '李%' order by math+english+chinese desc;




统计一个班级共有多少学生?
select count(*) from student;
select count(id) from student;
统计数学成绩大于80的学生有多少个?
select count(*) from student where math>80;
统计总分大于250的人数有多少?
select count(*) from student where math+english+chinese>250;


//细节 null不能被count
select count(chinese) from student;




统计一个班级数学总成绩?
select sum(math) from student;


统计一个班级语文、英语、数学各科的总成绩select sum(math),sum(english),sum(chinese) from student;


统计一个班级语文、英语、数学的成绩总和
select sum(math+english+chinese) from student;


统计一个班级语文成绩平均分
select sum(chinese)/count(*) from student;


求一个班级数学平均分?
select avg(chinese) from student;


求一个班级总分平均分?
select avg(math+english+chinese) from student;


求班级最高分和最低分(数值范围在统计中特别有用)
select max(math+english+chinese) from student;
select min(math+english+chinese) from student;
对订单表中商品归类后,显示每一类商品的总价


select product,sum(price) from orders group by product;


查询购买了几类商品,并且每类总价大于100的商品select product,sum(price) from orders group by product having sum(price)>100;




四、表约束
1.定义主键约束
create table demo1(
id int primary key,
        name varchar(40)


);
//1.插入空id
insert into demo1(name) values('aaa');
//2.不允许重复
insert into demo1(id,name) values(1,'aaa');
insert into demo1(id,name) values(1,'aaa');




2.定义主键自动增长
create table demo2(
id int auto_increment primary key,
        name varchar(40)


);


insert into demo2(name) values('aaa');
delete from demo2 where id = 3;
3.定义唯一约束
create table demo3(
id int auto_increment primary key,
        name varchar(40) unique 
);




insert into demo3(name) values('aaa');
4.定义非空
create table demo4(
id int auto_increment primary key,
        name varchar(40) not null
);


insert into demo3(password) values('123');


5.定义外键约束


create table department(
    id int auto_increment primary key,
    name varchar(30) not null
);


create table employee2(
    id int auto_increment primary key,
    name varchar(30) not null,
    salary  double, 
    department_id int,
    constraint department_id_FK foreign key(department_id) references department(id)
);

























Logo

更多推荐