创建:create      插入:insert  更新:update   

查询:select  删除:delete  修改:alter     销毁:drop

创建一个数据库:
  create database 数据库名 [其他选项];
  create database `samp_db`;
创建数据库表:
  create table 表名称(列声明);
  create table `students`
  (
    `id` int unsigned not null auto_increment primary key,
    `name` char(8) not null,
    `sex` char(4) not null,
    `age` tinyint unsigned not null,
    `tel `char(13) null default "-"
    )ENGINE=MyISAM charset=utf8;
向表中插入数据:
  insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

  insert into `students` set `name`='王刚',`sex`='男',`age`='20',`tel`='13811371377';
  insert into `students` values(NULL, "王刚", "男", 20, "13811371377");
查询表中的数据:
  select 列名称 from 表名称 [查询条件];
  select `name`,`age` from `students`;
或者使用通配符查询:
  select * from `students`;
按特定条件查询:
  select 列名称 from 表名称 where 条件;
  select * from `students` where `sex`="女";
  where 子句条件支持(=、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等,还可以对查询条件使用 or 和 and 进行组合查询)
  select * from `students` where `age` > 21;
  select * from `students` where `name` like "%王%";
  select * from `students` where `id`<5 and `age`>20;
更新表中的数据:
  update 表名称 set 列名称=新值 where 更新条件;
  将id为5的手机号改为默认的"-":

    update `students` set `tel`=default where `id`=5;

  将所有人的年龄增加1:

  删除id为2的行:

  删除所有年龄小于21岁的数据:

  删除表中的所有数据:

    delete from `students`;

创建后表的修改:
添加列:
  alter table 表名 add 列名 列数据类型 [after 插入位置];
  在表的最后追加列 address:

     alter table `students` add `address` char(60);
  在名为 age 的列后插入列birthday:

    alter table `students` add `birthday date after `age`;

  将表 tel 列改名为 telphone:

  将 name 列的数据类型改为 char(16):

  删除 birthday 列:

  重命名 students 表为 workmates:

  删除 workmates 表:

  删除 samp_db 数据库:

     drop database `samp_db`;

 

 

相关文章:

  • 2021-12-13
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2021-09-23
  • 2021-11-04
猜你喜欢
  • 2021-12-17
  • 2022-12-23
  • 2021-11-21
  • 2022-02-09
  • 2022-12-23
  • 2021-07-22
  • 2022-12-23
相关资源
相似解决方案