MYSQL数据库基本操作

数据库的创建

1.创建数据库

  • 创建一个名为hellword的数据库

create database hellword;

  • 创建一个使用utf8字符集的 db2 数据库

create database db2 charset=utf;

  • 创建一个使用utf字符集,并带校对规则的 db3 数据库

create database db3 charset=utf8 collate utf8_general_ci;

2.使用数据库

  • use+库名

eg:use hellword;

3.创建一个名为student的数据库表

create table student(
id int,
name varchar(32),
gender varchar(2)
);

4.向表中插入数据

insert into student (id, name, gender) values (1, '张三', '男');

insert into student (id, name, gender) values (2, '李四', '女');

insert into student (id, name, gender) values (3, '王五', '男');

5.查看相关

 查看系统默认字符集以及校验

  • show variables like 'character_set_database';
  • show variables like 'collation_database';

查看数据库支持的字符集校验规则

  • show collation;

查看数据表详细

select  *  from 表名

  • select  *  from student;

查看数据表结构

describe 表名

  • describe students;

查看当前数据库所有的表

  • show tables;

查看所有的数据库

  • show databases;

数据库的修改

1.修改表中的数据

update 表名 set 列名=新值 where 列名=旧值;

源数据表

修改student表中的一列数据

  • update student set id=2;

修改某行某列数据

  • update student set id=8 where name='李四';

2.删除数据库

drop database 库名

  • drop database hellword

3.删除数据库中的表

drop table 表名

  • drop table student

4.修改数据库中的表名

rename tables 旧表名 to 新表名

  • rename tables student to students;

5.修改数据表数据

<1>alter table 表名

  删除students表

  • alter table students;

<2>alter table 表名 drop 列名;

删除student表中的gender列

  • alter table students drop gender;

 

<3>delete from 表名 where 条件(列名=值);

删除student表中id小于8的数据

  • delete from students where id<7;

 

删除表中的数据,可以指定条件,如果不指定条件,则删除该表的所有数据.

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值