MySQL中关于数据库和数据表的常用操作

数据库的常用操作

1、查看数据库

 show databases; 				//查看或显示当前用户权限范围以内的数据库
 show databases like '%test%';	//使用like从句匹配合适的数据库
 show create database test;		//查看test数据库的定义声明

show databaseshow create database test
2、创建数据库

 create database test_01;
 create database if not exists test_01; //使用 if not exists 从句避免错误,因为MySQL 不允许在同一系统创建两个相同名称的数据库

create database

3、删除数据库

 drop database test;
 create database if exists test; // 使用 if exists 从句, 避免数据库不存在而报错

drop database
4、选择数据库

use 数据库名称;

use databse

数据表的常用操作

1、查看数据表

 show tables;			  	// 查看当前数据库下的所有表
 show tables like 'test%'	// 使用like从句进行模糊查询
 describe test;				//	查看某一个表的结构
 desc test;					// 作用同上,简写
 show create table 表名\G  	// 用来显示创建表时的CREATE TABLE语句

在MySQL的sql语句后加上\G,表示将查询结果进行按列打印,可以使每个字段打印到单独的行
show tables
desc table
解释下上图查询表结构的结果中的各个字段的含义:

  • Null:表示该列是否可以存储 NULL 值, 即初始值是否可以为空
  • Key:表示该列是否已编制索引。其中的名词有:PRI – 主键、UNI – 表示该列为unique索引的部分、MUL – 表示在该列中给定值允许出现多次
  • Default: 表示该列是否有默认值
  • Extra: 表示可以获取的与给定列有关的附加信息

show create table
2、创建数据表

 show tables;			  	// 查看当前数据库下的所有表
 create table student ( name varchar(20), age int, score int);

create table
3、删除数据表

 drop table student;
 drop table if exists student;

drop table
4、修改数据表

 alter table student add column math_score int first;			 // 在student的表中添加一列math_score(int类型),并且添加在第一列
 alter table student add column English_score int after age;	 // 在student的表中添加一列English_score (int类型),并且添加在列name之后
 alter table student modify name varchar(50); 					 //	修改name的数据类型
 alter table student drop name;						     		 // 删除name字段
 alter table student change math_score math varchar(10);		 // 修改字段名称和数据类型
 alter table student rename to stu;								 // 修改表名

alter table
table modify

table drop
table change
table rename

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值