数据库(二)

unsigned
    a.无符号的
    b.声明该数据列不允许负数
zerofill 默认启用(unsigned)
    a.0填充
    b.不足位数的用0来填充
auto_increment
    a.自动增长
    b.通常用于设置主键,且为整数类型
    c.可定义起始值和步长

1 alter table `表名` auto_increment = '往上调'  #  才会有效果


===========================修改表(alter table)==================================
1.修改表名
alter table `旧表名` rename as `新表名`;
例如:

1 alter table users rename as users1;

 

2.添加字段
alter table `表名` add 字段名 列类型[属性];
例如:

alter table Users1 add sex varchar(10);


3.修改字段
alter table 表名 modify 字段名 列类型[属性];
例如:

alter table Users1 modify userid int(4);


4.修改字段
alter table 表名 change 旧字段名 新字段名 列类型[属性];
例如:

alter table Users1 change userid  UserID int(4);


5.删除字段
alter table 表名 drop 字段名;
例如:

1 alter table Users1 drop sex
2 alter table Users1 drop primary key;    # 删除主键
3 alter table Users1 drop foreign key fk_LearnMySql_Users1;    # 删除外键


=========================================DML:insert\update\delete========================
1.insert ★省略字段,values要给全部的字段赋值★
语法:insert into `表名`[(字段1,字段2...)] values('值1','值2'...);
例如:

insert into `Users` values(default,'admin','admin','admin','系统管理员');


★字段名与values值顺序要一致★
语法:insert into 表名(字段1,字段2,字段3,字段4) values('值1','值2','值3','值4');
例如:

1 insert into Users(UserID,UserName,UserPassword,UserLevel) values(default,'Jesicca','0000','normal');


插入多条语句,★注意字段不为空的,一定要赋值★
语法:insert into 表名(字段1,字段2) values('值1','值2'),('值1','值2'),('值1','值2');
例如:

1 insert into Users(UserID,UserName,UserPassword,UserLevel) values(6,'Lily','1234','normal'),(7,'ivy','1235','normal'),(8,'Mary','1236','normal');


2.update    ★★set sql_safe_updates = 0;★★
语法:update 表名 set 字段1=值1,字段2=值2,... where 条件;
例如:

update Users set UserName='jack',UserPassword='12345678' where UserID=0008;


拼接字符串,或加减乘除运算
例如:

update Users set UserLevel = concat('姓名',UserLevel);


3.delete
语法:delete from 表名 where 条件;
例如:

delete from users where UserID = 0006;


删除整张表
语法:delete from 表名;
例如:

delete from users;


==================================where===============================
运算符:关系运算符,一元运算符,<>或!=,between...and,and,or,in,like

in
范例:至少一个值相等
select 字段1,字段2,...from 表名 where 字段x in (值1,值2,...);

like
范例:%,多个字符;_,单个字符

===============================truncate======================================
1.作用:【完全】清空表数据,但表结构、索引、约束不变
2.语法:truncate 表名
3.与delete区别:
    相同:都能删除数据,不删除表结构,truncate速度快
    不同:a.truncate重新设置auto_increment计算器
            b.truncate不会对事务有影响

================================事务=========================
1.将一组sql语句放在同一批次内去执行
2.如果一个sql语句出错,则该批次内的所有sql都将被取消执行

注意:MySQL事务处理只支持【InnoDB】和BDB数据表类型

3.事务ACID原则
    a.原子性(Atomic)
    b.一致性(Consist)
    c.隔离性(Isolated)
    d.持久性(Durable)
4.实现方法
set autocommit
    使用set语句改变自动提交模式
    set autocommit = 0;关闭
    set autocommit = 1;开启
注意:a.MySQL中默认是自动提交
      b.使用事务时应先关闭自动提交
start transaction
    开始一个事务,标记事务的起始点
commit
    提交一个事务给数据库
rollback
    将事务回滚,数据回到本次事务的初始状态
set autocommit = 1;
    还原MySQL数据库的自动提交
    
========================================================DQL=========================================
    
======================================distinct==========================
去掉结果中重复的记录
select distinct 字段1,字段2... from 表名
注意:all 默认,返回所有的记录

======================================连接查询===============================
1.内连接(inner join)
select 字段1,字段2 from table1 inner join table2 on table1.字段x = table2.字段y;
范例:

1 select st.`name`,su.subjectname,sc.score from student st
2 inner join score sc on st.StuID = sc.StuID
3 join `subject` su on su.`subjectid` = sc.`subjectid`;


2.外连接(outer join)
    a.左连接(left join)
        从左表(table1)中返回所有的记录,即便右表(table2)中没有匹配的行★★
    语法:select 字段1,字段2...from table1 left[outer]    join table2 on table1.字段x = table2.字段y;
    范例:

1 select st.stuid,st.`name`,sc.score from student st left outer join score sc
2 on st.stuid = sc.stuid;

   
    b.右连接(right join)
        从右表(table2)中返回所有的记录,即便左表(table1)中没有匹配的行★★
    语法:select 字段1,字段2...from table1 right[outer] join table2 on table1.字段x = table2.字段y;
    范例:

1 select st.stuid,st.`name`,sc.score from student st right outer join score sc
2 on st.stuid = sc.stuid;


=============================================order by================================
范例:

1 select st.`Name`,sc.subjectid ,score from student st inner join score sc
2 on st.StuID = sc.StuID
3 order by sc.Score desc;

    1.默认(asc)升序
    2.desc 降序
    
=============================================limit====================================
limit[m,]n 或 limit n offset m
    1.限制select返回结果行数
    2.m 制定第一个返回记录行的★偏移量★
    3.n 制定返回记录行的★最大数目★
注意:1.m 不指定则偏移量为0,从第一条开始返回前n条记录
      2.limit 【常用于分页显示】
范例:

1 1.select * from Score limit 5;    # 返回前5条记录
2 2.select * from Score limit 5,10;    # 返回6-15条记录


=============================================子查询===================================
范例:

1 select st.`name`,st.stuid  from student st
2 where st.StuID in (select sc.StuID from score sc where sc.score > 80
3 and sc.subjectid = (select su.SubjectID from `subject` su where subjectname = '高等数学'));


===============================================聚合函数=================================
1.count    记录总和
2.sum    一列的总和
3.avg
4.max
5.min
范例:

1 select count(sc.stuid) '总条数',max(sc.score)'最高分',min(sc.score)'最低分' ,avg(sc.score)'平均分'from Score sc;

     
综合:
select [all | distinct]{* | table.* |[table.field1[as name1]][,table.field2[as name2]][,...] }
from table1 [as name3]
[left | out | inner join table2]    # 联合查询
[where...]    # 指定结果需满足的条件
[group by...]    # 指定结果按照哪几个字段来分组
[having...]    # 过滤分组的记录需满足的次要条件
[order by...]    # 指定查询记录按一个或多个条件排序
[limit{[offset,]row_count | row_count iffset offset}];    # 指定查询的记录从哪条至哪条

[]    代表可选
{}    代表必须
#    MySQL语句的注释符,也可以用/*注释*/

====================================备份==================================
mysqldump
    1.转储数据库
    2.搜集数据库进行备份
    3.将数据库转移到另一个sql服务器
语法:mysqldump -h 主机名 -u 用户名 -p [options]数据库名
      [table1 table2 tabl3] > path/filename.sql
      



posted @ 2016-05-15 22:31  Ivy_Xu  阅读(175)  评论(0编辑  收藏  举报