mysql 数据库 基本操作(第一章)

来源:互联网 发布:怎样在淘宝上进货 编辑:程序博客网 时间:2024/06/15 10:06

由于职业发展,需要精通mysql,接下来,我向大家介绍下如何从不懂mysql到精通mysql

———序言

安装mysql 我就不说了,大家可以上网查下。


mysql  分为:

DDL:数据定义,意思就是定义不同的数据段、数据库、表、列、索引等,关键词是 create、drop、alter

DML:数据操作,意思就是用户对表里面的数据进行增删改查的操作,关键词是 select、update、delete、insert

DSL:数据控制,意思就是对数据库、表、列、字段的访问权限和安全级别,关键词是 grank,revoke


DDl详解:

创建数据库:sql: create database test;

删除表:drop database test;

查看数据库:show database ;

使用某一数据库:use test;

创建表:create table emp{

name varchar(20)

age int(10)

monery decimal(10,2)

}

删除表: drop table emp;

查看表:show tables;

查看表的定义:desc emp;

查看表的创建sql:show create table emp \G;

更改emp表的表名:alter table emp rename emp1;

更改emp表的name字段为ename:alter table emp change name ename varchar(30);

给emp表新增字段并且字段在什么位置:alter table emp add COLUMN sex char(5) alter age;

删除emp表的某个字段:alter table emp drop COLUMN age; 


DDL详解:

查看数据 select * from emp

添加某条数据:insert into emp (name,age,sex) values('小明',18,男)

修改某条数据:update emp set name = '小红', sex = '女' where 条件

删除某条数据:delete from emp where 条件 

查询使用,聚合数据:count(), min(), max(), sum(),  

with rollup 表明是否对分类 聚合后的结果进行再汇总

在某些情况下,子查询可以转换为表连接,例如:

selct * from emp where age in (select age from ag )

转换后:select emp.* from emp, ag where emp.age = ag.age 

还有一种情况就是 连个表按照一定的查询条件查询出来,然后结果合并,这时,需要用到union,union all

union all : 是将结果集直接合并在一起

union :是将结果集合并后再进行一次 distinct,去除重复记录后的结果

例子:select * from emp union select * from ag

select *  from emp union all select * from ag


DCL详解: DCL主要是DBA管理系统中的对象时所使用的

比如:grant select , insert on sakil.* to 'zl'@'localhost' identified by '123'

sakil(数据库名称)  zl(mysql 用户)localhost(地址) .*( 代表所有表)  indentified by '123' 用户密码

由于权限变更,需要将zl的权限收回 insert,只能对数据库进行 insert

revoke insert on sakil.* to 'zl' @ 'localhost';


 
原创粉丝点击