博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库笔记群集
阅读量:2048 次
发布时间:2019-04-28

本文共 5954 字,大约阅读时间需要 19 分钟。

07920170915------------------------------------------DDL-------------------------------------------1、DDL(Data Definition language):定义对数据库对象(库,表,列,索引)[1、create,2、drop,3、alter,4、rename]1)、显示数据库列表:show databases; 2)、创建数据库:create database 新数据库名;3)、使用数据库:use 数据库名;4)、显示数据库有多少张表:show tables;5)、创建一张表:drop table if exists 新表名;create table 新表名(id int primary key auto_increment,name varchar(50),add_time timestamp,descrtiption text);6)、显示表结构:desc 表名; 7)、删除表:drop table 表名;  8)、更改表名:alter table 表名 rename 新表名;9)、添加新字段:alter table 表名 add 新字段名 varchar(20);10)、更改字段:alter table 表名 change 字段名 新字段名 varchar(50);11)、删除字段:alter table 表名 drop 字段名;12)、修改数据库编码规则:alter database 数据库名 character set utf8;------------------------------------------DML-------------------------------------------2、DML(Data Manipulation Language):数据操纵语言[1、insert,2、delete,3、update,4、select,5、truncate(清空表)]1)、添加1[添加单行]:insert into employee (name,age,...) values ('佳佳','23',...);    添加2[添加多行]:insert into employee (name,age,...) values ('佳佳','23',...),('彩兰','22',...);    添加3[复制添加]:insert into employee (name,age,...) select * from people;[注:表employee和表people的结构要一致]3)、更改:update employee set salary=salary*1.21 where salary<5000;4)、删除1[清空删除]:delete from employee;     删除2[条件删除]:delete from employee where age>21;    删除3[条件删除]:delete from employee where name='佳佳' and salary=6666;    删除4[条件删除]:delete from employee where name='佳佳' or salary=8888 or name='彩兰';5)、清除:truncate table employee;6)、查询1[查询所有]:select * from employee;    查询2[别名查询]:select name 姓名,sex 性别,salary 薪水 from employees;    查询3[条件查询]:select * from employee where age>20;    查询4[去重查询]:select distinct salary from employee;    查询5[区间查询]:select * from employee where salary between 2000.00 and 5000.00;    查询6[模糊查询]:select * from employee where name like '林%';    查询7[嵌套查询]:select * from employee in(select * from employee where age=23 or age=33);    查询8[空值查询]:select * from employee where name is null;    查询9[排序查询]:select * from employee order by salary;[默认升序] | order by 字段名 desc;[降序]    查询10[分页查询]:select * from employee limit 10;[查询前10(1-10)]    查询11[分页查询]:select * from employee limit 10,2;[查询11、12]    查询12[组合查询]:select * from employee in (select age from employee where age<20);	    查询13[分组查询]:select name as 学生名,sum(score) as 总成绩 from student group by name(依据此分组);(查询每个学生的总成绩)    查询14[分组查询]:select name as 学生名,count(*) as 科目数 from student where score >80 group by name;(查询每个学生成绩大于80的科目数)     查询15[分组查询]:select name as 学生名,sum(score) as 总成绩 from student group by name(依据此分组) having sum(score)>200; (查询总成绩大于200的学生名字) 	            	    查询16[左右链接]:select employee.name 员工姓名,dept.name 部门 from employee left join dept on employee.number=dept.number;                      select employee.name 员工姓名,dept.name 部门 from employee right join dept on employee.number=dept.number;		      left/raight join...on左/右边的字段全都显示,右/左边根据字段显示,有就显示,没有就null;--------------------------------------------DCL-----------------------------------------3、DCL(Data Contrl Language):数据控制语言,定义对数据库,表,字段,用户访问权限和安全级别。[1、GRANT,2、REVOKE]-------------------------------------------事物控制---------------------------------------4、Transaction Contrl:事物控制[1、start transaction,2、commit,3、roollback,4、savepoint]1)、概念:一组逻辑执行单元(多条sql语句),要么全部执行成功,要么失败。   ACID是数据库事务正确执行的四个基本要素的缩写,即原子性(Atomicity)、一致性(Consistency)、隔离性[数据并发问题](Isolation)、持久性(Durability)。2)、作用:保证数据的完整性。3)、特点(ACID):(1)、原子性、(2)、一致性(数据前后总和不变)、(3)、隔离性(解决多事务同一时刻操作同一数据问题)、(4)、持久性4)、使用步骤:    (1)、禁用自交:set autocommit=0;    (2)、开启事务:start transaction;    (3)、编写语句:sql1;sql2;sql3;...;    (4)、事务回滚:rollback;[将数据库表的状态重新初始化回sql语句操作前的状态]    (5)、提交事务:commit;5)、例子[银行转账事务管理]:update account set money = money-100 where id='A';                            update account set money = money+100 where id='B';------------------------------------------约束-------------------------------------------	1、数据库约束[保证数据的完整性]:1)、列级(字段)约束写法 2)、表级约束写法1、not null:非空约束[表示该字段不能为空,只能为列级约束]2、unique:唯一约束[表示该字段的值不能有重复值,可以进行表级约束,可以定义约束名,便于以后更改]3、primary key (`id`):主键约束[行的唯一标识,支持表级写法(有约束名,便于以后操作)和列级写法]5、组合约束:  primary key (`user_id`,`role_id`);6、外键约束:constraint `外键别名` foreign key (`role_id`) references `org_role` (`id`)[外键只能指向主键,类型要一致]7、增加外键约束:alter table 表名 add constraint `外键别名` foreign key (`从表外键`) references `主表名` (`主表主键`)8、级联约束删除:constraint `外键别名` foreign key (`从表id`) references `主表名` (`主表id`) on delete cascade-------------------------------------------索引------------------------------------------1、索引作用:提高查询性能,加速查询2、使用场景:针对某个字段做频繁查询,则将字段作为索引。[相当于二级查询,表数据量大时才有必要建立索引]3、建立索引:1、系统自动[primary key、unique、]	     2、自己设置[create index 索引名 on 表名(字段名)]4、删除索引:drop index 索引名 on 表名-------------------------------------------视图------------------------------------------1、视图的概念:虚拟映射表2、特点:1)、不能用来操作数据(删除、添加、更新);2)、便于查询数据(多表复杂数据)3、用法:create or replace view 视图名 as 查询语句4、删除:drop view 视图名-------------------------------------------函数------------------------------------------1、char_length(字段名)[获取字段字符长度]:select * from employee where char_length(name) > 3;2、select now();[获取系统当前时间]3、count(*)[统计总行数]:select count(*) from employee;4、select max(字段)[获取最大值]:select max(salary) 最高薪水 from employee;5、select min(字段)[获取最小值]:...6、select sum(字段)[统计数据总和]:select sum(salary) 工资总和 from employee;7、select avg(salary)[求平均数]:select svg(salary) 平均工资 from employee;8、concat('字段','xxx')[链接字符串]:select concat('字段','xxx') from employee; 9、ifnull(字段,expre2):select ifnull(name,'佳佳') as name,age,salary from employee;[如果expr1为null,则返回expr2,否则返回expr1]10、nullif(expre1,expre2):select nullif(name,'小米手机') as age,salary from employee;[如果expr1和expr2相等,则返回null,否则返回expr1]11、if(expre1,expre2,expre3):select if(isnull(name),'名为空','名不为空') as name,age,salary from employee;[三目运算]12、select 字段名 case 字段名 when xx1 then xx1 when xx2 then xx2 else xx3 end from 表名;-----------------------------------------删除表中重复记录--------------------------------------1、创建临时表:create table temp(select distinct * from target);2、清空目标表:delete from target;3、填充目标表:insert into target select from temp;4、删除临时表:drop table temp;

转载地址:http://tjeof.baihongyu.com/

你可能感兴趣的文章
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【深度学习】GRU的结构图及公式
查看>>
【python】re模块常用方法
查看>>
【JavaScript】call()和apply()方法
查看>>
【JavaScript】箭头函数与普通函数的区别
查看>>
前端面试题
查看>>
【JavaScript】常用方法记录
查看>>
C++ 数据存储类型
查看>>
39. Combination Sum
查看>>
剑指Offer 1.二维数组中的查找
查看>>
剑指offer 2.重建二叉树
查看>>
剑指offer 3.二叉树中和为某一值的路径
查看>>
剑指offer 4.替换空格
查看>>
剑指offer 5.从尾到头打印链表
查看>>
剑指offer 6.用两个栈实现队列
查看>>
剑指offer 7.旋转数组的最小数字
查看>>
剑指offer 8-11.斐波那契数列 跳台阶 变态跳台阶 矩形覆盖
查看>>
剑指offer 12.二进制中1的个数
查看>>