数据库mysql的基本操作

操作文件夹(数据库)

增:create database db1 charset utf8;
查:show databases;
改:alter database db1 charset latin1;
删除: drop database db1

查看当前所在的数据库
select database();

操作文件(表)

先切换到文件夹下:use db1
     增:create table t1(id int,name char);
     查:show tables
     改:alter table t1 modify name char(3);
         alter table t1 change name name1 char(2);
     删:drop table t1;

    查看表结构
    describe t1;

修改表结构

https://book.apeland.cn/details/460/

表的内容操作

1,增

1 insert into [表名] values(1,‘wu‘),(2,‘zhi‘),(3,‘bin‘);
2 insert into [表] (列名,列名....) values (值,值,值....),(值,值,值....);
3 insert into [表1] (列名,列名...) select 列名,列名... from 表2; #将表1复制给表二
1 delete from [表]   #清空表
2 delete from [表] where id =1 and name=‘wu‘;   #按条件删除
1 update [表] set name = ‘wang‘ where id =1;   #按条件修改
1 select * from [表]  #显示表中所有的列和数据
2 select * from [表] where id >1 ; #按条件查询
其他
#对条件查询
slect * from [表] where id > 5 and name != ‘wuzhibin‘;
slect * from [表] where id between 4 and 9;
select * from [表] where id in (1,3,5);
select * from [表1] where id in (select id from  [表2]) # 条件是表2中的ID
1 #通配符查询
2 select * from [表] where name like ‘wu%‘ ; #以wu开头后面可以匹配多个字符
3 select * from [表] where name like ‘wu_‘ ; # 以wu开头后面最多匹配一个字符
1 #查询限制
2 select * from [表] limit 5 ; #前面5行数据
3 select * from [表] limit 4,5; #从第五行开始的5行
4 select * from [表] limit 5 offset 4  # 从第武汉开始的5行
#排序查询
select * from [表] order by [列] asc;  #从小到大
select * from [表] order by [列] desc;  #从大到小
select * from [表] order by [列1] asc ,[列2] desc ; 列1相等在根据列2排序
1 #两张表组合查询
2 select name from [表1]  union select name from [表2];  #自动去重
3 select name from [表1] union all select name from [表2]; #不去重
1 #连表操作
2  select t1.id ,t1.name ,t2.name from t1,t2 where t1.nid = t2.id;

相关推荐