Mysql数据库基本操作

本篇文章记录了Mysql数据库的一些常规操作指令,便于使用数据库时查找。
col_name列名称 tbl_name表名称 operator运算符 valcoldb_name数据库名称 col_attr数据类型 file_loc文件位置

1.获取数据:
select col_name from tbl_name;
select * from tbl_name;//匹配所有列
select distinct col_name from tbl_name;//仅返回不同值

2.获取满足特定条件的数据:
select col_name from tbl_name where col 运算符 值;
例子:
select * from orders where id = 3;
select * from orders where id = 3 or id = 4;
select * from orders where name like 'N%';//选取制定模式
select * from orders where 1 between 3;//选取两值之间的数据

3.对数据进行排序:
select col from tbl_name order by col asc;//生序排列
select col from tbl_name order by col desc;//降序排列

4.插入数据:
insert into tbl_name values(val1,val2,...);
insert into tbl_name(col1,col2,...) values(val1,val2,...);//插入指定列

5.修改数据:
update tbl_name set col_name = val1 where col = val2;

6.删除数据:
delete from tbl_name where col = val;

7.规定返回数据数目:
select col_name from tbl_name limit val;

```select top 50 percent * from tbl_name;//选取50%的记录```

8.创建数据:
```create database db_name;//创建数据库```
```create table tbl_name(col1 attr1,col2 attr2,....);```

9.撤销表:
```drop table tbl_name;```
```drop database db_name;```
```truncate table tbl_name;//仅删除表格中数据

10.alter已有表添加、删除、修改:
```alter table tbl_name add column col_name col_attr;```
```alter table tbl_name drop column col_name col_attr;```
```alter table tbl_name alter column col_name col_attr;```

11.备份复件:
```select col_name into tbl1 from tbl2;```

12.字段编码格式改变:
例子:
```alter table tbl_name modify col_name col_attr character set gbk;//改为gbk编码模式```

13.查看数据:
```show full columns from tbl_name;//查看表单字符集设置```
```show tables;//查看所在数据库所有表单```
```show databases;```
```describe tbl_name;//查看表单详细信息```

14.导入文件:
```source file_loc;//导入外部sql文件```
例子:
```source D:\bookmark.sql;```
```load data local infile 'file_loc' into table tbl_name;//txt格式文件导入数据库```
例子:
```load data local infile 'D:\excel.txt' into table tbl_name character set utf8 fields terminated by',';```

    原文作者:CiferZ
    原文地址: https://www.jianshu.com/p/9ee80217c329
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞