SQL语句

// 1.创建表
语法:
create table 表名(字段1 约束1 约束2,字段2 约束1 约束2);

// 上边的SQL语句的含义是:第一次创建表,第二次如果再执行这个SQL语句就会报错

create table if not exists
表名(字段1 约束1 约束2,字段2 约束1 约束2);

// 第二个SQL语句:如果这个表没有存在的情况下才会执行

// 事例
需求:创建一个student表,表中的字段有学号,姓名,年龄;学号的约束条件:作为主键,自增,不能为空;姓名默认为'无名氏';年龄大于16岁

create table student (s_id integer primary key autoincrement not null,s_name text default '无名氏',s_age integer check (s_age > 16));


// 2.插入数据
语法:
insert into 表名(字段1, 字段2, 字段3)values(值1, 值2, 值3);

// 事例:插入学生姓名,年龄
insert into student(s_name, s_age)values('小明', 18);
insert into student(s_name, s_age)values('MBBoy', 38);

// 3.更新数据
语法:
update 表名 set 字段名1 = 修改值1, 字段名2 = 修改值2 where 条件;

事例:
update student set s_age = 25 where s_age = 18;

// 4.删除数据
语法:
delete from 表名 where 条件;

事例:
// 需求:删除年龄为10岁的学生
delete from student where s_age = 38;

// 5.查询数据
语法:
select 要查找的字段 from 表名 where 条件;

事例:
// 查询为小明的所有信息
select s_age from student where s_name = '小明';

原文地址:https://www.cnblogs.com/yyd100286480/p/5460179.html