mysql笔记1

<!--

学习的链接

https://www.cnblogs.com/cxx8181602/p/9525950.html

-->



 

<!-- 连接数据库 -->

mysql -h 地址 -P 端口 -u 用户名 -p 密码

mysql -h 127.0.0.1 -P 3306 -u root -p 123456

 

<!-- 退出数据库 -->

exit;

 

<!-- 数据库操作 -->

<!-- 关键字create创建数据库 -->

create database 数据库名 [数组库选项];

 

create database test default charset utf8 collate utf8_bin;

<!-- 数据库选项:字符集和校对规则 -->

字符集:一般为utf8;

校对规则:⑴ci结尾的:不分区大小写 ⑵cs结尾的:区分大小写 ⑶bin结尾的:二进制编码进行比较

 

<!-- 关键字show 查看当前有哪些数据库 -->

show database;

<!-- 查看数据库的创建语句 -->

show create database 数据库名

 

<!-- 关键字alert修改数据库选项信息 -->

alert database 数据库名 [新的数据库选项]

alert database test default charset gbk;

 

<!-- 关键字drop删除数据库 -->

drop database 数据库名;

 

<!-- 关键字use 进入指定的数据库 -->

use test;


 

<!-- 表的操作 -->

<!-- 关键字create创建数据表 -->

create table test(

字段1 字段1类型 [字段选项]

字段2 字段2类型 [字段选项]

字段3 字段3类型 [字段选项]

)表选项信息;

 

create table test (

id int(10) unsigned not null auto_increment comment 'id',

conent varchar(100) not null default '' comment '内容',

time int(10) not null default 0 comment '时间',

primary key (id)

)engine=InnoDB default charet=utf8 comment '测试表'

语法解析(下文MySQL列属性单独解析):

如果不想字段为NULL可以设置字段的属性为NOT NUL,在操作数据库时如果输入该字段的数据为NULL,就会报错.

AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1.

PRIMARY KEY关键字用于定义列为主键.可以使用多列来定义主键,列间以逗号分隔.

ENGINE 设置存储引擎,CHARSET 设置编码, comment 备注信息.

 

<!-- 关键字show 查询当前数据库下有哪些数据表 -->

show tables;

 

<!-- 关键字like模糊查询 -->

通配符:_ 可以代表任意的单个字符 % 可以代表任意的字符

 

show tables like '模糊查询表名%';

 

<!-- 查看表的创建语句 -->

show create table 表名;

 

<!-- 查看表的结构 -->

desc 表名;

 

<!-- 关键字drop 删除数据表-->

drop table [if exists] 表名;

drop table if exists test;

 

<!-- 关键字alter 修改表名 -->

alter table 旧表名 rename to 新表名

 

<!-- 关键字add 增加一列 -->

alter table test add 新列明 字段类型 [字段选项];

alter table test add name char(10) not null default '' comment '用户名字';

 

<!-- 关键字drop删除一列 -->

alter table test drop 字段名;

 

<!-- 关键字modify修改字段类型 -->

alter table test modify 字段名 新的字段名 [新的字段选项];

alter table test modify name username varchar(100) not null default '' comment '新的用户名';

 

<!-- /*关键字:first 修改字段排序,把某个字段放在最前面*/ -->

alter table 表名 modify 字段名 字段类型 [字段选项] first;

例如: alter table test modify name varchar(100) not null default 'admin' comment '最前面' first;

 

<!-- /*关键字:after 修改字段排序,字段名1放在字段名2的后面*/ -->

alter table 表名 modify 字段名1 字段类型 [字段选项] after 字段名2;

例如: alter table test modify name varchar(100) not null default 'admin' comment 'time字段后面' after time;

 

<!-- /*关键字:change 重命名字段*/ -->

alter table 表名 change 原字段名 新字段名 新的字段类型 [新的字段选项];

例如: alter table test change name username varchar(50) not null default '' comment '用户名字';

 

<!-- /*修改表选项*/ -->

alter table 表名 表选项信息;

例如: alter table test engine Myisam default charset gbk; --修改存储引擎和修改表的字符集



 

<!-- #数据操作 -->

<!-- /*关键字:insert 插入数据(增)*/ -->

insert into 表名(字段列表) values(值列表);

例如: create table user(

id int(10) unsigned not null auto_increment comment 'id',

name char(10) not null default '' comment '名字',

age int(3) not null default 0 comment '年龄',

primary key (id)

)engine=InnoDB default charset=utf8 comment='用户表';

<!-- --插入数据 -->

insert into user(id,name,age) values(1,'admin_a',50);

insert into user(name) values('admin_b');


 

<!-- /*关键字:select 查询数据(查)*/ -->

select *[字段列表] from 表名[查询条件];

 

例如: select * from user;

<!-- --查全部字段用*代替 -->


 

select name from user where age>0;

<!-- --查name字段,age大于0 -->


 

<!-- /*关键字:delete 删除数据(删)*/ -->

delete from 表名[删除条件];

例如: delete from user where age<1;

<!--删除age小于1数据 -->


 

<!-- /*关键字:update 修改数据(改)*/ -->

update 表名 set 字段1=新值1,字段n=新值n [修改条件];

例如: update user set age=100 where name='admin_a';

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值