数据库表操作

创建数据库表

使用 create table 语句可完成对表的创建, create table 的常见形式:

createtable 表名称(列声明);

以创建 students 表为例, 表中将存放 学号(id)、姓名(name)、性别(sex)、年龄(age)、联系电话(tel) 这些内容:

      create table students

      (

            id int unsigned not nullauto_increment primary key,

            name char(8) not null,

            sex char(4) not null,

            age tinyint unsigned not null,

            tel char(13) null default"-"

      );

向表中插入数据

insert语句可以用来将一行或多行数据插到数据库表中,使用的一般形式如下:

insert[into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

查询表中的数据

select语句常用来根据一定的查询规则到数据库中获取数据, 其基本的用法为:

select列名称 from 表名称 [查询条件];

 例如要查询 students 表中所有学生的名字和年龄, 输入语句 select name, age from students; 执行结果如下:

 mysql> select name, age from students;

      +--------+-----+

      | name  | age |

      +--------+-----+

      | 王刚  |  20 |

      | 孙丽华 |  21 |

      | 王永恒 |  23 |

      | 郑俊杰 |  19 |

      | 陈芳  |  22 |

      | 张伟朋 |  21 |

      +--------+-----+

      6 rows in set (0.00 sec)

     mysql>


按特定条件查询:

where 关键词用于指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;

 以查询所有性别为女的信息为例, 输入查询语句: select * from students where sex="女";


更新表中的数据

update 语句可用来修改表中的数据, 基本的使用形式为:

update 表名称 set 列名称=新值 where 更新条件;


删除表中的数据

delete 语句用于删除表中的数据, 基本用法为:

delete from 表名称 where 删除条件;

lter table 语句用于创建后对表的修改, 基础用法如下:


添加列

基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];


修改列 基本形式: alter table 表名 change 列名称 列新名称 新数据类型;


删除列

基本形式: alter table 表名 drop 列名称;


重命名表

基本形式: alter table 表名 rename 新表名;


删除整张表

基本形式: drop table 表名;


删除整个数据库

基本形式: drop database 数据库名;





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值