Appearance
📣 本笔记基于 sqlite ,其他 sql 服务器需要修改
表格创建
sql
create table stu (id int, name char, score float);
create table if not exists stu (id int, name char, score float);表格删除
sql
drop table stu;插入记录
sql
insert into stu values (1, 'bob', 99);
insert into stu (id, name) values (9, 'scott');查看记录
sql
select * from stu;
select * from where id<3 and score>90;
select id, name from stu where score > 90;修改记录
sql
update stu set score=60 where id=1;删除记录
sql
delete from stu where id = 1;
delete from stu; --删除所有主键 primary key
sql
create table stu (id int primary key, name char, score float);拷贝
sql
create table b as select * from a;
create table stu1 as select id, name, score from stu;增加列
sql
alter table stu add aolumn score int;修改表名
sql
alter table stu rename to stuinfo;修改字段名(列名)
sql
-- 不支持直接修改字段名
-- 1. 重命名
alter table stuinfo rename to stu;
-- 2. 新建修改名字后的表(新建一个 a)
create table stuinfo (name char, age1 int, sex char, score int);
-- 3. 从旧表 b 中取出数据,插入到新表 a 中
insert into stuinfo select * from stu;删除列
sql
-- 不支持直接删除列
-- 1. 创建一个表 b,复制旧表 a 需要保留的字段信息
create table stu as select name, age1, sex from stuinfo;
-- 2. 删除旧表a
drop table stuinfo;
-- 3. 修改新表 b 的名字 a
alter table stu rename to stuinfo;