MySQL使用教程

SQL语句:

(1)DDL(数据定义语言)<结构>

create(增) drop(删) alter(改) show(查)

(2)DML(数据操纵语言)<数据>

insert(增) delete(删) update(改) select(查)

(3)DCL(数据控制语言)
grant(授权) revoke(回收)

数据库:

一、创建数据库(create database)

//CY1207为库名
create database CY1207;  //创建库

create database if not exists CY1207;  //如果不存在则创建库

二、删除数据库(drop database)

drop database CY1207;//删除数据库
drop database if exists CY1207;  //如果存在则删除

三、查询时数据库(show database)

show database;  //查询数据库命令

表:

一、创建表(create table)

create table 表名(   字段名  字段类型 [字段约束],……,……,……   ) ;

字段约束

主键:“非空且唯一” primary key

外键:foreign key

唯一约束

非空约束:not null

默认为空:default null

//创建表,stu为表名
create table stu(
    id varchar(20) PRIMARY KEY,
    name varchar(20),
    age int,
    sex enum("man","woman")       //枚举
);

二、查看表(show table)

show create table stu;//查看类型信息
desc stu;  //查看字段信息(命令)

三、修改表(alter table)

对表的字段的操作:

/*
    change  修改字段名/字段约束
    alter table 表名 change 旧字段 新字段 新字段类型 [新字段约束];
*/
alter table stu change name stu_name varchar(25);

/*
    modify  修改字段类型
    alter table 表名 modify 字段 字段类型;
*/
alter table stu modify name varchar(25);

/*
    add  添加新字段
    alter table 表名 add 新字段 新字段类型 [新字段约束];
*/
alter table stu add score floot not null;

/*
    drop  删除字段
    alter table 表名 drop 字段;
*/
alter table stu drop score;

修改表名:

/*
    rename 修改表名
    alter table 旧表名 rename 新表名;

*/
alter table stu rename student;

 四、删除表(drop table)

/*
    drop table 表名;  //删除
    drop table if exsits 表名;//如果存在则删除
*/
drop table student;
drop table if exsits student;

 数据:

一、插入数据(insert)

insert 插入数据(其他也可以插入数据:load和source(多数据插入),replace(delete+insert/insert))

insert into student(id,name,age,sex) values("001","zhangsan","19","man");

二、删除数据(delete)

delete(会记录日志)truncate (删除不记录日志)

//delete from student [where];  where条件筛选
delete from student where id = "001";

三、修改数据(update)

//where 条件筛选
update student set age = 18 where id = "001";

四、查看数据(select)

大致结构如下图:

select id,name
from student
where id="001";

 查询数据还分为多种复杂情况,上面是普通查询还有许多查询方式:去重查询、排序查询、分组查询、多表查询、连接查询(外连接(左、右、中)、内链接)、联合查询等等,由此可见数据库最重要的就在查询,所以在下一篇博客具体说。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值