MySQL数据库增删改查

数据库

创建数据库

create database 数据库名;

创建数据库的时候设置字符集,老数据库的默认字符集:latin
而数据库如果设置不对之后会很麻烦

create database 数据库名 charset utf8;

删除数据库

drop database 数据库名;

修改数据库

修改数据库名有点麻烦,就我知道有两种方法:
第一种是: 将旧数据库导出,然后导入时候改名。
第二种是: 直接到数据库目录修改数据库名。

查看数据库

show databases;

创建表

create table 表名 (
	id int not null auto_increment primary key (id)),	//第5个字段表示自增,6主键
	name char(32) ,
	age int , 
	date date, 
	);

查看表

use 数据库名;
show table 表名;

修改表

alter table 旧表名 rename to 新表名;
rename table 旧表名 to 新表名;

删除表

删除表

扫描二维码关注公众号,回复: 3472895 查看本文章

drop table 表名;

表结构

添加字段

alter table 表名 add 字段名 类型 是否为空 默认…

alter table now_table add sex char(32) ;

删除字段

alter table 表名 drop 字段名;

修改字段

必须指定新字段类型

alter table 表名 change 旧字段名 新字段名 类型 是否为空 默认值;

alter table now_table change sex gender char;

查询字段

desc 表名;

表内容

插入内容

insert into 表名 (字段1,字段2,.....) values 
(字段1数据,字段2数据,....),
(字段1数据,字段2数据,....),
....一次可以插入多条数据
;

删除内容

delete from 表名 where 判断内容;

delete from now_table where age=24;

修改内容

update 表名 set 修改字段1=修改内容,修改字段2=修改内容2 where 判断;

update now_table set date='1991-06-12' where age=27;

查看内容

查看全部内容

select * from 表名;
select * from user\G;

查看指定字段(一次可以查询多个字段)

select 字段1,字段2,… from 表名;

判断查询

SELECT column_name,column_name
FROM table_name
[WHERE Clause]
[OFFSET M] [LIMIT N]

例子

select * from xxxx where time='2018-09-01' ;		//查看时间等于 2018-09-01 的数据
select * from xxxx where time like '2018-09%' ;		//查看时间在 2018-09 这个月份的数据
select * from xxxx where shuzi < 3 ;		//查看 shuzi 小于3的所有数据
select * from xxxx limit 10 offset 3 ;			//查看10条数据,忽略前三条,从第四条开始

查询排序

SELECT 字段1,字段2,..... FROM 字段内容1, 字段内容2,...
ORDER BY 字段1, [字段2...][ASC [DESC]]

asc 是升序, desc 是降序。
例子

select user,password,host from user order by user asc;

show

查看数据库创建过程

show create database 数据库;

查看表的创建过程

show create table 数据库;

远程连接

mysql -h ip地址 -u 用户名 -p密码

猜你喜欢

转载自blog.csdn.net/xphouziyu/article/details/82946837