数据定义(DDL):用户定义、删除和修改数据模式
数据查询(DQL):用于查询数据
数据操纵(DML):用于增、删、改数据
数据控制(DCL):用于控制数据访问权限
基本常用命令:(命令前都有一个.)
1. .exit: 退出
2. .help:查看手册
3. .tables:查看有哪些表
4. .schema chat:查看表的结构
SQL由命令组成,每个命令以分号(;)结束(很重要)
创建一个表:
在SQL中,创建和删除数据库对象的语句一般被称为数据定义语言(data definition language,DDL),操作这些对象中数据的语句称为数据操作语言(data manipulation language,DML)。创建表的语句属于DDL,用CREATE TABLE命令,如下定义:
CREATE [TEMP] TABLE table_name (column_definitions [, constraints]);
例如:
create table if not exists student(name text, id integer, sex text);
说明:
1.text为字符型(字符串,字符),integer为整型数
2.创建表时,可以指定一个关键字,表示该关键字下不允许有同名存在
例如:
create table if not exists student(name text primary key, id integer, sex text);
该表的关键字为name,即不允许有同名存在
改变表的结构
例如:
alter table student rename to stu; //修改表名
alter table student add column score integer; //增加一列
插入数据
insert into student(name, id, sex, score) values ("test", "1", "f", "100");
插入数据可以选定插入的项,不一定一次插入所有的项,对于上表中的四项而言:
insert into student(name, id, score) values ("test", "1", "100");
那么,我们也可以插入某一项:
update student set sex = "f" where name = "test";
查看
select *from student; //查看所有记录
select name from student; //查看某一列
select name from student where score > 3; //查看满足某一条件的记录
select name from student where score (not)between 2 and 4;
select name from student where score in (2, 3, 4);
删除
delete from student where name = "test"; //删除某条记录