【MySQL语句】

MySQL语句

数据库操作

  • 显示当前mysql软件中,当前用户可以看到的数据库
show databases;
  • 创建数据库
create database 数据库名;
  • 删除数据库
drop database 数据库名;
  • 使用数据库,选择数据库
use 数据库名;
  • 查看当前数据库下的所有表格
show tables;

表结构的操作

  • 查看某个表的结构
desc 表名称;
  • 创建表格
			create table 表名称(
     字段名  数据类型  约束,
    字段名  数据类型  约束, 
   字段名  数据类型  约束
);

- 修改表结构
增加一列

alter table 表名称  add 【column】 字段名  数据类型 【not null】【default 默认值】;

删除一列

alter table 表名称 drop 【column】 字段名;

修改数据类型,非空约束,默认值

alter table 表名称 modify 【column】 字段名  新的数据类型 【not null】【default 默认值】;

修改列名称

alter table 表名称 change 【column】 旧字段名 新的列名称  新的数据类型 【not null】【default 默认值】;

表的重命名

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

修改约束

增加主键

alter table 表名称 add 【约束名】 primary key(字段列表);

删除主键

alter table 表名称 drop primary key;

增加唯一性约束

alter table 表名称 add 【约束名】 unique (字段列表);

删除唯一性约束

alter table 表名称 drop index   约束名;

数据的操作

  • 增加
insert into 表名称(字段列表) values(值列表);
值列表要与字段列表一一对应
insert into 表名称  values(值列表);
默认所有列都要插入数据,值列表的数量与顺序要与表结构一一对应
insert into 表名称(字段列表) values(值列表), (值列表);
  • 修改
update 表名称 set 字段名 = 字段值,字段名 = 字段值 .....    where 条件;
  • 删除
删除全表的数据
	delete from 表名称;
	truncate 表名称;
		效率高,但不能回滚
删除部分行
	delete from 表名称  where 条件;
  • 数据的查询
查询全部行全部列
	select * from 表名;
查询部分列,所有行
	select 字段列表  from 表名;
查询部分列,部分行
	select 字段列表  from 表名   where  条件;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值