sql删除语句_Part 3 | SQL学习:删除行

学习内容:

SQL第15课:删除行

表:Movies

ec2a90adc329d9fe4f6676d9e255b940.png

Exercise 15 — Tasks

  1. This database is getting too big, lets remove all movies that were released before 2005.
  2. Andrew Stanton has also left the studio, so please remove all movies directed by him.

需要掌握的SQL语句

删除带条件的语句

DELETE FROM mytable
WHERE condition;

注意:与UPDATE上一课中的语句一样,建议您SELECT首先在查询中运行约束,以确保删除正确的行。没有适当的备份或测试数据库,很容易不可撤销地删除数据,因此请始终阅读您的DELETE语句两次并执行一次。

输出结果

1.delete FROM movies

where year<2005

c0d709db588e150a4086882d99be8870.png

前置步骤,先用select查找出需要删除的行

select title,year

from movies

where year<2005

2ba91e1701ca8656baf1d4da0802acc7.png

2.delete from movies

where director="Andrew Stanton"

ee9c6211b65e57920635d47e1837584e.png

总结:

单词delete不要书写错;凡是默认的函数或者语句,SQL语句输入时会变化颜色~

create table if not exists tb_log_gps ( id bigint not null, device_id varchar not null, platform_id varchar, location varchar, happen_time varchar, create_time TIMESTAMP NOT NULL DEFAULT now() ); CREATE INDEX idx_tb_log_gps_id ON tb_log_gps(id); CREATE INDEX idx_tb_log_gps_happen_time ON tb_log_gps(happen_time); CREATE OR REPLACE FUNCTION insert_log_gps_partition_func() RETURNS TRIGGER AS $$ DECLARE date_text TEXT; insert_statement TEXT; date_part DATE := '2000-01-01'; date_next DATE := '2000-01-01'; BEGIN SELECT SUBSTRING(NEW.happen_time,1,10) INTO date_text; insert_statement := 'INSERT INTO tb_log_gps_' || date_text ||' VALUES ($1.*);'; EXECUTE insert_statement USING NEW; RETURN NULL; EXCEPTION WHEN UNDEFINED_TABLE THEN date_part := ('''' || date_part('year'::text, to_date(NEW.happen_time,'yyyy-MM-dd hh24:mi:ss')) || '-' || date_part('month'::text, to_date(NEW.happen_time,'yyyy-MM-dd hh24:mi:ss')) || '-' || date_part('day'::text, to_date(NEW.happen_time,'yyyy-MM-dd hh24:mi:ss')))::DATE; date_next := date_part + '1 day'::interval; EXECUTE 'CREATE TABLE IF NOT EXISTS tb_log_gps_' || date_text || '(CHECK(' || 'happen_time' || '>= ''' || date_part::text || ''' AND ' || 'happen_time' || '< ''' || date_next::text || ''')) INHERITS (tb_log_gps);'; EXECUTE 'create index idx_tb_log_gps_' || date_text || '_happen_time on tb_log_gps_' || date_text || '(happen_time);'; EXECUTE 'create index idx_tb_log_gps_' || date_text || '_id on tb_log_gps_' || date_text || '(id);'; EXECUTE insert_statement USING NEW; RETURN NULL; END; $$ LANGUAGE plpgsql VOLATILE; DROP TRIGGER IF EXISTS insert_tb_log_gps_partition_trigger ON tb_log_gps; CREATE TRIGGER insert_tb_log_gps_partition_trigger BEFORE INSERT ON tb_log_gps FOR EACH ROW EXECUTE PROCEDURE insert_log_gps_partition_func(); 插入数据报错,这个分表触发器哪里错了
05-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值