创建数据库表
使用 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 数据库名;