MySQL的库,表,记录的增删改查

1 MySQL的基础指令

服务器,数据库,表,记录,字段的关系:
一台服务器中有多个数据库
一个数据库中有多个表
一个表中有多条记录
一条记录中有多个字段

2 数据库的操作

2.1 创建数据库
语法:
create database `数据库名` [选项];

例:
create database `itsource`;

备注:
什么时候使用反引号? 当数据库名与MySQL的关键发生冲突的时候,就要使用反引号将数据库名引起来.
create database create;		# 失败
create database `create`;	# 成功

[选项]: 
设置字符集 charset=utf8
create database `itsource2` charset=utf8;
2.2 查询数据库
a.查询所有数据库
show databases; 

b.根据条件查询 like "%_"
%	表示任意多个任意字符
_	表示一个任意字符

查询出所有以it开头的数据库:
show databases like 'it%';
查询出数据库名为6个字符的:
show databases like '______';
查询出数据库名包含source的数据库:
show databases like '%source%';

c.查询建库语句
show create database `库名`;
show create database `itsource`;

注意:
什么时候用database,什么时候用databases?
如果查询结果只有一个,或者在SQL中已指定数据库名时,使用 database
如果查询结果可能会有多个,使用 databases
2.3 修改数据库选项
语法:
alter database `数据库名` [新的选项];

例:
修改`itsource`的字符集为 gbk
alter database `itsource` charset=gbk;
2.4 删除数据库
语法:
drop database `数据库名`;

例:
删除`itsource2`数据库
drop database `itsource2`;

3 表的操作

操作表之前,必须先指定数据库

切换数据库或者指定数据库:
use `库名`; 
3.1 创建表
语法:
create table `表名`(
	字段名1 字段类型 字段属性,
    字段名2 字段类型 字段属性,
    字段名3 字段类型 字段属性,
    字段名n 字段类型 字段属性   
)[选项];

备注:
a.字段类型
常用的数据类型: 
int			整数型 
varchar(M)	 字符串类型, M 代表字符串长度

b.最后一个字段的后面不用跟逗号

c.[选项]
1. 引擎 engine
	innodb	高级引擎(高级功能:支持事务)
	myisam	功能简单(速度快)
	engine=innodb
	engine=myisam
2. 字符集 charset
	charset=utf8
	charset=gbk
3. 备注 comment
	comment='备注信息'
	comment='学生表'
	

例:
创建一个学生表:
create table `student`(
	id int,
    name varchar(20),
    sex varchar(1),
    age int
)engine=innodb charset=utf8 comment='学生表';

create table `student1`(
	id int,
    name varchar(20),
    sex varchar(1),
    age int
)engine=myisam charset=gbk comment='学生表1';

3.2查看表
a.查看所有表
show tables;

b.根据条件查询表 like
show tables like '%_';
查询以stu开头的表:
show tables like 'stu%';
查询表名为4个字符的表:
show tables like '____';

c.查看建表语句
show create table `表名`;
show create table `student`;

d.查看表结构
desc `表名`;
desc `student`;

3.3 修改表
a.修改表选项
语法:
alter table `表名` [新的选项];

例:
修改student表的引擎为myisam
alter table `student` engine=myisam;

修改student表的字符集为gbk
alter table `student` charset=gbk;

修改student表的备注为'学生表2'
alter table `student` comment='学生表2';

b.修改表名
语法:
rename table `旧表名` to `新表名`;

例:
修改student1的表名为student2
rename table `student1` to `student2`;


3.4 删除表
语法:
drop table `表名`;

删除表: student2
drop table `student2`;

4 记录的操作

4.1 增加
语法:
insert into `表名`(字段列表) values(值列表);

a.值与字段一一对应
insert into `student`(id,name,sex,age) values(1,'zhangfei','M',200);
insert into `student`(id,name,sex,age) values(1,'张飞','男',200);
insert into `student`(name,sex,age) values('关羽','男',230);

b.不写字段列表,值必须全部传入
insert into `student` values(2,'刘备','男',250);

c.一次性传入多条记录
insert into `student` values
(4,'大乔','女',20),
(5,'小乔','女',18),
(6,'貂蝉','女',19);


注:
set names gbk;	# 告知MySQL服务器端,本地的编码为gbk
4.2 查询
查询所有记录:
select * from `表名`;
查询学生表的所有记录:
select * from `student`;

语法:
select 字段列表 from `表名` [where 条件表达式];
a.字段列表: 要查询哪个字段,就写哪个字段
例:查询出student表中的name字段
select name from `student`;

b.where 条件表达式: 对查询结果进行过滤
条件表达式返回的结果为一个Boolean值,如果为True,就保留;如果为False,则过滤掉;
例:
查询出student表中age<20的记录
select * from `student` where age<20;

查询出student表中姓名包含'乔'的记录
select * from `student` where name like '%乔%';

查询出student表中姓名包含'乔',并且年龄小于20的记录
select * from `student` where name like '%乔%' and age < 20;

查询出student表中性别为女,或者年龄小于20的记录
select * from `student` where sex='女' or age<20;



4.3 修改
语法:
update `表名` set `字段名1`='值1',`字段名n`='值n' [where 条件表达式];

例:
修改student表中张飞的年龄为25:
update `student` set `age`=25 where name='张飞';

修改student表中刘备的年龄为22,性别为女:
update `student` set `age`=22,`sex`='女' where name='刘备';

修改student表中的所有记录的id为0:
update `student` set `id`=0;



4.4 删除
语法:
delete from `表名` [where 条件表达式];

例:
删除student表中sex为M的记录
delete from `student` where sex='M';

删除student表中所有记录
delete from `student`;


注意:
在工作中,一般都要写 where 条件表达式

SQL = DDL + DML

数据定义语言DDL: create drop show alter
数据管理语言DML: insert update select delete

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值