mysql随笔记

1.直接创建数据库 xiaozhang
create database xiaozhang;

2.查看当前在哪个库里边
select database();

3.进入库的操作
use 库名;

4.判断是否存在,如果不存在则创建数据库 db
create database if not exists db;

5.创建数据库并指定字符集为 gbk
create database db1 default character set gbk;

修改库的字符集编码
alter database 库名 default character set gbk;

6.查看某个库是什么字符集;
SHOW CREATE DATABASE 库名;

7.查看当前mysql使用的字符集
show variables like ‘character%’;

8.查看数据库中的所有表。 show tables;
查看表结构 desc 表名;
查看创建表 show create table 表名;或者 show create table 表名\G

  1. 修改表名 rename table 旧表名 to 新表名;
给表添加一列   alter table    表名     add  列名  数据类型;

给表最前面添加一列 alter table 表名 add 列名 类型 first;

给表某个字段后添加一列 alter table 表名 add 列名 类型 after 字段名;

修改列的类型 alter table 表名 modify 列名 新类型;

修改列名 alter table 表名 change 旧列名 新列名 类型;

删除列 alter table 表名 drop 列名;

修改字符集 alter table 表名 character set 字符集;

删除表 drop table 表名;
drop table if exists 表名;

  1. 1.普通的插入表数据
    insert into 表名 (字段名) values (字段对应值);
    (job,name) (‘警察’,‘张三’)

insert into 表名 values (所有字段对应值);

    2.蠕虫复制(将一张表的数据复制到另一张表中)
    insert into 表名1 select *from 表名2;

    insert into 表名1(字段名1,字段名2)select 字段名1, 字段名2 from 表名2;


    复制表结构 并不会复制数据  不加where 1=2时所有数据都复制过去 
    create table 新表名 as select * form 旧表名 where 1=2;

    create table 新表名 like 旧表名;  不会把数据复制过去 
    会表结构全部复制过来
    create table 库名.新表名 like 旧表名;

    3.建表复制
     create table 表名1 as select 字段名1,字段名2 from 表名2;

    4.一次性插入多个数据
     insert into 表名 (字段名) values ('对应值1'),('对应值2'),('对应值3');

CRETATE TABLE employee (
empno INT(5) unsigned zerofill auto_increment primary key comment ‘雇员编号’,
ename VARCHAR(20) comment ‘雇员姓名’,
job VARCHAR(20) comment ‘雇员职位’,
mgr INT comment ‘雇员上级编号’,
hiredate DATE comment ‘雇员日期’,
sal DECIMAL(7,2) comment ‘薪资’,
deptnu INT comment ‘部门编号’
);

create table student(
id int(3) auto_increment primary key comment ‘学号’,
name varchar(20) comment ‘姓名’,
grade float comment ‘成绩’,
gender char(1) comment ‘性别’
);

  1. 修改 update 表名 set 字段名1=值1 where 字段名=值;

             update  表名  set 字段名1=值1,set 字段名2=值2  where 字段名=值;            
    

    删除 delete from 表名 where 字段名=值;

           truncate  table 表名;不会记录删除操作,会把表占用的空间恢复到最初,不会删除定义
           delete from 表名;   会把删除的操作记录给记录起来,以便数据回退,不会释放空间也不会删除定义
          drop table 表名;      删除整张表,释放表占用的空间   
    
  2. where条件下查询

    1.简单查询 select * from employ;
    (表名)
    select ename,job as ename_job from employ;
    (字段名) (新字段名)

    2.精确查询 select * from employ where ename=‘校长’;
    select * from employ where sal != 999.99;
    不等于
    select * from employ where sal <> 999.99;

                      select * from employ where sal > 666.66;
    

    3.模糊条件查询
    show variables like ‘%aracter%’;

                    select * from employ where like '军%'; 
                    select min(grade) from student where name not like "son%" and name like "%ng";
    

    4.范围查询
    select * from employ where sal between 10000 and 999;

    5.选择匹配查询 select * from employ where job in (‘老师’,‘军人’,‘学生’);

    6.消除重复值
    select distinct(job) from employ;

    统计查询
    select count(*) from employ;

    和 查询 select sum(sal) from employ;

最大最小查询 select max(sal) from employ;
select max(grade) from student where id in (‘4’,‘5’,‘6’);

                  select * from employ where sal=(select max(sal) from employ);

平均值 查询 select avg(sal) from employ;

选择字段 查询 select deptnu,job,count() from employ group by deptnu,job;
筛选 查询 select deptnu,job,count(
) from employ group by deptnu,job having deptnu=10;
select deptnu,job,count(*) as 总数 from employ group by deptnu,job having 总数>=1;

   排序 查询   select * from employ order by sal;
          select deptnu,job,count(*) as 总数 from employ group by deptnu,job having 总数>=1 order by sal;默认从低到高
          select deptnu,job,count(*) as 总数 from employ group by deptnu,job having 总数>=1 order by sal desc;  倒序
     
  限制查询  select * from employ limit 0,3;    limit n,m  n:代表起始条数值,默认为0;m代表:取出的条数
                  select * from yy614.student limit 1,2;
  exists查询   select * from 表名  where exists (select 1 from 表名2 where 条件);
           例:    select * from dept a  where exists (select 1 from employ b where a.deptnu=b.deptnu);

insert into student
(id,name,grade,gender)
values
(1,‘songjiang’,40,‘男’),
(2,‘wuyong’,100,‘男’),
(3,‘qinming’,90,‘男’),
(4,‘husanniang’,88,‘女’),
(5,‘sunerniang’,66,‘女’),
(6,‘wusong’,86,‘男’),
(7,‘linchong’,92,‘男’) ,
(8,‘yanqing’,90,null);

select count() from student;
select count(
) from student where grade>=60;
select distinct(gender) from student;

select sum(grade) from student;
select sum(grade) from student group by gender having gender=‘男’;

select avg(grade) from student;
select avg(grade) from student group by gender having gender=‘女’;

select max(grade) from student;
select max(grade) from student where id in (‘4’,‘5’,‘6’);

select min(grade) from student;
select min(grade) from student where name not like “son%” and name like “%ng”;

左连接查询与右连接查询

左连接说明: left join 是left outer join的简写,左(外)连接,左表(a_table)的记录将会全部表示出来,
而右表 (b_table)只会显示符合搜索条件的记录。右表记录不足的地方均为NULL。 左表记录不足的地方均为NULL。
右连接说明:right join是right outer join的简写,与左(外)连接相反,右(外)连接,左表(a_table)只会显示符合 搜
索条件的记录,而右表(b_table)的记录将会全部表示出来。

列出部门名称和这些部门的员工信息,同时列出那些没有的员工的部门
select a.dname,b.*
from
dept a
left join
employee b
on a.deptnu=b.deptnu;

内连接:获取两个表中字段匹配关系的记录
主要语法:INNER JOIN 表名 ON 条件;
select
a.addr
from
dept a
inner join
employee b on a.deptnu=b.deptnu and b.ename=‘张飞’;
select a.addr from dept a , employee b where a.deptnu=b.deptnu and b.ename=‘张飞’;

Union
select * from employee a where a.job=‘销售员’ union select * from employee a where a.job=‘文员’;

select * from employee where sal > (select sal from employee where ename =‘安琪拉’ );

创建触发器的语法:

create trigger 触发器名称 after/before insert/update/delete on 表名
for each row
begin
sql语句;
end

添加外键约束的注意事项:
添加的外键列与另一个表的唯一索引列(一般是主键)的数据类型不同
要添加外键的表类型与另一个表的存储引擎是不是都为innodb引擎
设置的外键与另一个表中的唯一索引列(一般是主键)中的值不匹配

语法:foreign key (字段名) references 关联的表名(关联表的字段名)
注意:主键跟外键的字段类型一定要相

create table的方法:
CREATE TABLE employee ( empno int(11) NOT NULL COMMENT ‘雇员编号’,
ename varchar(50) DEFAULT NULL COMMENT ‘雇员姓名’, job varchar(30) DEFAULT NULL,
mgr int(11) DEFAULT NULL COMMENT ‘雇员上级编号’,
hiredate date DEFAULT NULL COMMENT ‘雇佣日期’,
sal decimal(7,2) DEFAULT NULL COMMENT ‘薪资’,
deptnu int(11) DEFAULT NULL COMMENT ‘部门编号’,
PRIMARY KEY (empno),
foreign key (deptnu) references dept(deptnu)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

alter table的方法: alter table employee add foreign key (deptnu) references dept(deptnu);

删除自增:alter table test change id id int(7) unsigned zerofill not null;
修改数据约束:alter table sc change sno sno int(10) not null comment ‘学号’;

insert ignore into sc
(sno,cno,degree)
values
(20050101,‘C01’,92),
(20050101,‘C02’,85),
(20050101,‘C03’,88),
(20050202,‘C02’,90),
(20050202,‘C03’,80);

48
create table writers (
w_id int(10) unique key auto_increment not null zerofill comment ‘编号’,
w_name varchar(20) default not null comment ‘作者姓名’,
w_address varchar(50) default null’作者地址’,
w_ageint default not null comment ‘年龄’,
w_note text comment ‘说明’ ,
primary key (w_id),
unique key uniqidx (w_id)
);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhazhahui~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值