mysql基础day01

1.MySQL基础指令

1.1创建数据库

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

1.2查询数据库

1.查询所有数据库
show databases;

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



3.查询建库语句
show create database `数据库名`;

1.3修改数据库选项

语法:
alter database `数据库名` [新选项];

例子:
修改'itsource'的字符集为 gbk
alter database `itsource` charset=gbk;

1.4删除数据库

语法:
drop database `数据库名`;

2.表操作

2.1创建表

操作表,现指定数据库
切换数据库或者指定数据库
use `库名`;


语法:
create table `表名`(
	字段名1 字段类型 字段属性,
    字段名1 字段类型 字段属性
)[选项];

字段类型:
int			整数型
varchara(M)	 字符串类型,M 代表字符串长度

[选项]
1.引擎 engine
	innodb 高级引擎
	myisam 功能简单(速度快)
2.字符集 charset
	charset=utf8
	charset=gbk
3.备注 comment
	comment='备注'
	
例子:
create table itsource.`student`(
	id int,
    name varchar(20),
    sex varchar(2),
    age int
)engine=innodb charset=utf8 comment='学生表';

2.2查询表

查询所有
show tables;
根据条件查询表 "%_"
show tables like '%';
show tables like '_';
查看建表语句
show create table `表名`;

查看表结构
desc `表名`;

2.3修改表

1.修改选项
alter table `表名` [新的选项];
例子:
修改引擎
alter table `student` engine=myisam;

修改字符集
alter table `student` charset=gbk;
2.修改表名
rename table `旧表名` to `新表名`;

例子:
rename table `student` to `student1`;

2.4删除表

drop table `表名`;

删除学生表
drop table `student`;

3.记录操作

3.1增加记录

语法:
insert into `表名`(字段列表) values(值列表);
1.值与字段一一对应
insert into `student`(id,name,sex,age) values(1,'张三','男',20)
2.不写字段列表,值要全部传入
insert into `student` values(2,'李四','男',30);

3.只传部分值
insert into `student`(name,age) values('张飞',50);
4.传入多条记录
insert into `student` values(),(),();


set names gbk;  # 告诉MySQL服务器端,本地编码为gbk

3.2查看记录

语法:
select 字段列表 from `表名` [where 条件表达式];
查看表所有记录
select * from `student`;
查询字段列表:
select name from `student`;

条件表达式:对查询结果进行过滤
返回结果为布尔值(boolean),如果为True就保留,如果为flase就过滤掉;
例子:
查询年龄小于30的记录
select * from `student` where age<30;

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

3.3修改 记录

语法:
update `表名` set `字段名` = '值' [where 条件表达式];

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

修改student表中多个字段
update `表名` set `字段名` = '值',`字段名n` = '值' [where 条件表达式];

3.4删除记录

语法:
delete from `表名` [where 字段表达式];

例子:
删除student表中张飞记录
delete from `student` where name='张飞';

注意:工作中修改表记录都要加 where 条件
发布了20 篇原创文章 · 获赞 0 · 访问量 202

猜你喜欢

转载自blog.csdn.net/weixin_46054381/article/details/103930774