超详细的mysql数据库使用指南

一、数据库基本操作

	-- 链接数据库
	mysql -uroot -p
	mysql -uroot -pXXX


	-- 关闭数据库链接
	exit/quit/ctrl+d


	-- 显示时间
	select now();


	-- 显示版本
	select version();


	-- 查看所有的数据库
	show databases;


	-- 创建数据库
	-- create database 数据库名 charset=utf8;
	create database python charset=utf8;


	-- 查看创建数据库语句
	-- show create database 数据库的名字
	show create database python;


	-- 查看当前使用的数据库
	select database();


	-- 删除数据库
	-- drop database 数据库名 (如果有特殊字符的话用tab上面的那个键包起来~~)
	drop database python;


	-- 使用数据库
	-- use 数据库名
	use python;


-- 数据表操作

	-- 查看数据库中的表
	show tables;


	-- 创建数据表
	-- auto_increment 表示自动增长
	-- not null 表示不能为空
	-- default 默认值
	-- create table 数据表名字 (字段 类型 约束 [, 字段 类型 约束])
	-- yyy的id后面全是约束
	create table xxx(id int, name varchar(30));
	create table yyy(
		id int primary key not null auto_increment,
		name varchar(30)
		);


	-- desc 数据表的名字
	desc xxx;


	--创建 students 表(id、 name、 age、 high、gender、 cls_id)
	-- unsigned: 无符号
	create table students(
		id int unsigned not null auto_increment primary key,
		name varchar(30),
		age tinyint unsigned default 0,
		high decimal(5, 2),
		gender enum("男", "女", "中性", "保密") default "保密",
		cls_id int unsigned
	);


	-- 插入数据
	insert into students values(0, "suger", 18, 188.88, '男', 0);

	
	-- 查看数据
	select * from students;


	-- 创建classes表(id, name)	
	create table classes(
		id int unsigned not null auto_increment primary key,
		name varchar(30)
	);


	-- 修改表结构-添加字段
	-- alter table 表名 add 列名 类型
	alter table students add birthday datetime;


	-- 修改表结构-修改字段:不重命名版
	-- alter table 表名 modify 列名 类型和约束
	alter table students modify birthday date;


	-- 修改表结构-修改字段:重命名版
	-- alter table 表名 change 旧列名 新列名 类型及约束
	alter table students change birthday birth date default "2000-01-01"


	-- 修改表结构-删除字段
	-- alter table 表名 drop 列名
	alter table students drop high;


	-- 删除表 
	-- drop table 表名
	drop table xxx;


	-- 查看创建数据表过程
	-- show create table 表名
	show create table students;

二、数据表的操作

	-- 查看数据库中的表
	show tables;


	-- 创建数据表
	-- auto_increment 表示自动增长
	-- not null 表示不能为空
	-- default 默认值
	-- create table 数据表名字 (字段 类型 约束 [, 字段 类型 约束])
	-- yyy的id后面全是约束
	create table xxx(id int, name varchar(30));
	create table yyy(
		id int primary key not null auto_increment,
		name varchar(30)
		);


	-- desc 数据表的名字
	desc xxx;


	--创建 students 表(id、 name、 age、 high、gender、 cls_id)
	-- unsigned: 无符号
	create table students(
		id int unsigned not null auto_increment primary key,
		name varchar(30),
		age tinyint unsigned default 0,
		high decimal(5, 2),
		gender enum("男", "女", "中性", "保密") default "保密",
		cls_id int unsigned
	);


	-- 插入数据
	insert into students values(0, "suger", 18, 188.88, '男', 0);

	
	-- 查看数据
	select * from students;


	-- 创建classes表(id, name)	
	create table classes(
		id int unsigned not null auto_increment primary key,
		name varchar(30)
	);


	-- 修改表结构-添加字段
	-- alter table 表名 add 列名 类型
	alter table students add birthday datetime;


	-- 修改表结构-修改字段:不重命名版
	-- alter table 表名 modify 列名 类型和约束
	alter table students modify birthday date;


	-- 修改表结构-修改字段:重命名版
	-- alter table 表名 change 旧列名 新列名 类型及约束
	alter table students change birthday birth date default "2000-01-01"


	-- 修改表结构-删除字段
	-- alter table 表名 drop 列名
	alter table students drop high;


	-- 删除表 
	-- drop table 表名
	drop table xxx;


	-- 查看创建数据表过程
	-- show create table 表名
	show create table students;

三、增删改

1.增加

-- 1.全列插入
-- insert [into] 表名 values(...)
-- 主键字段 可以用 0 null default 来占位
-- 向 classes表中插入 一个班级
insert into classes values(0, "abc");

-- 向 students 表中插入 一个学生的信息
insert into students values(0, "aaa", 20, '女', 1, '1900-01-01');
insert into students values(null, "aaa", 20, '女', 1, '1900-01-01');
insert into students values(default, "aaa", 20,<
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值