- DDL
- create 创建数据库和表等对象
- drop 删除数据库和表等对象
- alter 修改数据库和表等对象的结构
- DML
- select 查询表中数据
- insert 向表中插入数据
- update 更新表中的数据
- delete 删除表中数据
- DCL
- commit 确认对数据库中的数据进行变更
- rollback 取消数据库中数据进行的变更
- grant 赋予用户操作权限
- revoke 取消用户操作权限
创建数据库
create database shop;//创建的数据库名为shop
查看有哪些数据库
show database;
选择数据库
use shop;
创建表
create table product (列名 类型 约束,列名 类型 约束,···) primary key(列名);//创建一个名为product的表
/*类型有:
integer 数字
char(*) ‘’ 字符串
varchar(*) ‘’ 可变字符串
data(*) ‘*-*-*’ 日期
*/
/*约束:not null*/
删除数据库
drop datebase shop;
删除表
drop table product;
添加列
alter table product add column (列名 类型 约束);
删除某一列
alter table product drop column 列名;
向表中插入数据
insert into product values ('0001','T恤','衣服',1000,'2009-10-11');
变更表名
rename table 原表名 to 更新名;
查看表中所有的列
select * from product;
查看表中的相关列
select 列名,列名 from product;
查看时为列设置别名
select 原列名 as 现列名 form product;
查看时为某一列去除重复项
select distinct 列名 from product;
有条件的查看
select 列名,列名 from product where 列名 = 条件; //= 等于 <> 不等于
注释
/*这是注释*/
计算,基于列的
select 列名,列名 * 2 from product;
/*所有关于NULL的计算值都为NULL*/
/*四则运算
+ - * /
*/
/*比较运算符
= <> >= >> <= <
*/
当然,也可以加not
select 列名,列名,列名 from product where not 列名 = 条件;
不能对null使用运算符,但可以这样
select 列名,列名,列名 from product where 列名 is null;
或
select 列名,列名,列名 from product where 列名 is not null;
多条件查询
select 列名,列名 from product where 列名 = 条件 and 列名 <> 条件;
/* and 和 or */