mysql使用学习笔记

本文档详细介绍了如何连接MySQL数据库,包括创建和删除用户、查看和创建数据库、管理表结构、执行查询操作以及数据的增删改查。还涵盖了表的修改、数据的插入和删除,以及各种查询技巧,如条件查询、排序查询和模糊查询。是MySQL数据库管理的基础教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 连接mysql
mysql -h localhost -u root -p xxxxx
mysql -h ip -u root -p xxxxx
# 创建账号
create user "user name" @localhost identified by "password";
create user "test_user" @localhost identified by "root";
# 删除账号
drop user 用户名@地址ip;
drop user test_user@localhost;
# 查看所有的数据库
show databases;
# 创建数据库
create database weihang_databases_test2;
create database weihang_databases_test2 default charset utf8;
# 进入特定数据库
use xxxxxxxxx;
# 查看当前数据库的所有表
show tables;
# 数据类型
# 数据类型种类 数字(int,tinyint,smallint,float,double),字符串(char(个数)varchar(个数))时间(DATE,TIME,DATETIME),枚举enum(值只能是枚举中的元素),集合set(值只能是结合元素的组合)

# 在当前数据库中创建一个表
create table 表名(列名 数据类型 约束, 列名 数据类型 约束,···)engine=innodb default charset=utf8;

create table weihang_test_table(test_user_id int,test_user_name varchar(255), test_user_age varchar(255));
# 约束类型
# UNIQUE
# NOT NULL
# PRIMARY KEY
# 查看当前数据库中某个表的结构
desc 表名;
desc weihang_test_table;
show create table 表名;



# 查看表数据
select * from 表名;
select * from 数据库名.表名;
select * from weihang_test_table;

select 列名,列名··· from 表名;
select name,age from students;
select test_user_name,test_user_age from weihang_test_table;
# 条件查询
select 列名 from 表名 where 列名(id等) >/</!= value;
select 列名 from 表名 where 条件;
select 列名 from 表名 where 条件1 and 条件2;
select 列名 from 表名 where 条件1 or 条件2;
select * from caiwu where age=20;
select * from caiwu where age=20 and name="liming";
select * from caiwu where age=20 or name="liming";
# 模糊查询
select 字段 from 表名 where 字段 like ‘%数据%’;
# 模糊查询caiwu表中姓名字段含有三的记录行
select * from caiwu where name like '%三%';
# 查看caiwu表中id不等于1的记录
select * from caiwu where id!=1;
# 查询yunwei表中性别为女,或年龄为23的记录
select * from yunwei where sex='woman' or age=23;
# 查询yunwei表中,年龄为22-25之间的记录
select * from yunwei where age in(22,23,24,25);
# 排序查询
# 将caiwu表中记录按年龄从大到小查询
select * from caiwu order by age desc;
# 查询caiwu表中,年龄为21-23的记录,使用算数运算符表示为age>=21 and age<=23,使用范围运算啧表示为age between 21 and 23。
select *from caiwu where age between 21 and 23;

# 修改表名
alter table 表名 rename to 新表名;

# 修改字段的数据类型
alter table 表名 modify 字段名 新数据类型;
# 修改字段名(列名),也可修改数据类型
alter table 表名 change 字段名 新字段名 数据类型;
# 增加字段
alter table 表名 add 字段名 数据类型 约束条件;
# 删除字段
alter table 表名 drop 字段名;
alter table 表名 drop primary key; 删除表中主键
alter table 表名 add 列名 数据类型 primary key;添加主键
alter table 表名 add primary key(列名);设置主键
alter table 表名 add column 列名 数据类型 after 列名;在某一列后添加主键
# 表中添加行数据(注意数据类型,字符串类型需要加引号)
insert into 表名(列名1,列名2···) values(值1,值2···),(值1,值2···),(值1,值2···); 
insert into weihang_test_table(test_user_id,test_user_name,test_user_age) value(3, "weihang", "18");
insert into 表名(列名1,列名2···) values(值1,值2···),(值1,值2···),(值1,值2···); 
# 删除表
drop table 表名;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值