MySql入门操作命令
登陆
mysql -u -p
退出
exit
一.mysql的库可以比做成一个文件夹
1.增(创建库)
语法:
(;)分号作为结束符
create database 数据库名字 charset 编码格式(注意这里编码不需要加引号);
实例:
点击查看代码
create database dahai1 charset utf-8;
语法:
drop database 数据库名字;
实例:
点击查看代码
drop database dahail;
语法:
alter database 需要修改的数据库名字 charset 字符编码;
实例:
点击查看代码
alter database dahai1 charset gbk;
查看所有库:
点击查看代码
show databases;
语法:
show create database 数据库名字;
实例:
点击查看代码
show create database dahai1;
首先想操作表,一般我们需要先切换的库中才可以对表进行操作,
语法:
use 库名
实例:
点击查看代码
use dahai1;
1.切换的数据库中操作:
语法:
create table 表名(字段名 字段类型(宽度),字段名 字段类型(宽度)...);
实例:(这里int后面不加宽度因为int的宽度仅为显示宽度后面会将)
点击查看代码
create table t1(id int,name varchar(11));
语法:
create table 库名.表名(字段名 字段类型(宽度),字段名 字段类型(宽度)...);
实例:
点击查看代码
create table dahai1.t2(id int,name varchar(11));
语法:
drop table 表名;(注意这里不写表名就是全部删除)
实例:
点击查看代码
drop table t2;
3.改(一般不会改)
添加字段
语法:
alter table 表名 add 字段 类型(宽度);
实例:
点击查看代码
alter table t1 add sex char(11);
1.查看表结构
语法:
desc 表名;
实例:
点击查看代码
desc t1;
语法:
show create table 表名;
实例:
点击查看代码
show create table t1;
语法:
show tables;
4.查看自己所在的库;
语法:
show database();
三.改表字段的类型:
1.改字段的类型(不改字段)
语法:
alter table 表名 modify 字段 新类型(宽度);
实例:
点击查看代码
alter table t1 modify name char(11);
语法:
alter table 表名 change 老字段 新字段 新类型(宽度);
实例:
点击查看代码
alter table t1 change sex se varchar(11);
语法:
rename table 表名 to 新表名;
实例
点击查看代码
rename table t1 to ta;
1.增:
1.为想要的字段添加值,多于的会插入空。
语法:
insert into(可省略) 表名(字段1,字段2) values
(第一行数据,没有插入的字段会为空),
(第二行数据,没有插入的字段会为空),
(第三行数据,没有插入的字段会为空);
实例:
点击查看代码
insert into t1(id,name) values
(1,'dahai'),
(2,'xiaoming'),
(3,'xiaohong');
语法:
insert into 表名 values
(第一行数据,给所有字段插入,记录必须与表的字段数量还有类型要一致),
(第二行数据,给所有字段插入,记录必须与表的字段数量还有类型要一致),
(第三行数据,给所有字段插入,记录必须与表的字段数量还有类型要一致);
实例:
点击查看代码
insert into t1 values
(4,'xiaolan','man');
语法:
delete from 表名 where 条件(没有写条件是全部删除);
实例:
点击查看代码
delete from ta where id = 1;
语法:
truncate 表名
3.改
1.直接修改字段
语法:(注意这里修改的是一个字段里面所有的记录)
update 表名 set 字段名 = 修改值
实例:
点击查看代码
update ta set se ='man';
语法:
update 表名 set 字段名=修改值 where 条件;
实例:
点击查看代码
update ta set se='man' where id =3;
语法:
`update 表名 set 字段名 =修改值 where 条件 in('第几条','第几条');`
实例:
点击查看代码
update ta set se='man' where name in('xiaohong','xiaolan');
4.查
1.查看所有的记录
语法:
select * from 表名;
实例:
点击查看代码
select * from ta;
select 指定字段 from 表名;
实例
点击查看代码
select id,name from ta;
语法:
select * from 表名 where 条件;
实例:
点击查看代码
select * from ta where id=2
语法:
select * from 库名.表名;
实例:
点击查看代码
select * from dahai1.ta;
浙公网安备 33010602011771号