2008-05-27: UPDATE Statement (2008-05-07)

Skip Headers

PL/SQL User's Guide and Reference
10g Release 1 (10.1)

Part Number B10807-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Feedback

Go to previous page
Previous
Go to next page
Next
View PDF

UPDATE Statement

The UPDATE statement changes the values of specified columns in one or more rows in a table or view. For a full description of the UPDATE statement, see Oracle Database SQL Reference.

Syntax

Description of update_statement.gif follows
Description of the illustration update_statement.gif

Keyword and Parameter Description


alias

Another (usually short) name for the referenced table or view, typically used in the WHERE clause.


column_name

The column (or one of the columns) to be updated. It must be the name of a column in the referenced table or view. A column name cannot be repeated in the column_name list. Column names need not appear in the UPDATE statement in the same order that they appear in the table or view.


returning_clause

Returns values from updated rows, eliminating the need to SELECT the rows afterward. You can retrieve the column values into variables or host variables, or into collections or host arrays. You cannot use the RETURNING clause for remote or parallel updates. If the statement does not affect any rows, the values of the variables specified in the RETURNING clause are undefined. For the syntax of returning_clause, see "DELETE Statement".


SET column_name = sql_expression

This clause assigns the value of sql_expression to the column identified by column_name. If sql_expression contains references to columns in the table being updated, the references are resolved in the context of the current row. The old column values are used on the right side of the equal sign.


SET column_name = (subquery3)

Assigns the value retrieved from the database by subquery3 to the column identified by column_name. The subquery must return exactly one row and one column.


SET (column_name, column_name, ...) = (subquery4)

Assigns the values retrieved from the database by subquery4 to the columns in the column_name list. The subquery must return exactly one row that includes all the columns listed.

The column values returned by the subquery are assigned to the columns in the column list in order. The first value is assigned to the first column in the list, the second value is assigned to the second column in the list, and so on.

The following example creates a table with correct employee IDs but garbled names. Then it runs an UPDATE statement with a correlated query, to retrieve the correct names from the EMPLOYEES table and fix the names in the new table.

-- Create a table with all the right IDs, but messed-up names.
CREATE TABLE e1 AS
   SELECT employee_id,
          UPPER(first_name) first_name,
          TRANSLATE(last_name,'aeiou','12345') last_name
   FROM employees;

BEGIN
-- Display the first 5 names to show they're messed up.
   FOR person IN (SELECT * FROM e1 WHERE ROWNUM < 6)
   LOOP
      dbms_output.put_line(person.first_name || ' ' || person.last_name);
   END LOOP;
   UPDATE e1 SET (first_name, last_name) =
      (SELECT first_name, last_name FROM employees
         WHERE employee_id = e1.employee_id);
   dbms_output.put_line('*** Updated ' || SQL%ROWCOUNT || ' rows. ***');
-- Display the first 5 names to show they've been fixed up. 
   FOR person IN (SELECT * FROM e1 WHERE ROWNUM < 6)
   LOOP
      dbms_output.put_line(person.first_name || ' ' || person.last_name);
   END LOOP;
END;
/

DROP TABLE e1;

sql_expression

Any valid SQL expression. For more information, see Oracle Database SQL Reference.


subquery

A SELECT statement that provides a set of rows for processing. Its syntax is like that of select_into_statement without the INTO clause. See "SELECT INTO Statement".


table_reference

A table or view that must be accessible when you execute the UPDATE statement, and for which you must have UPDATE privileges. For the syntax of table_reference, see "DELETE Statement".


TABLE (subquery2)

The operand of TABLE is a SELECT statement that returns a single column value, which must be a nested table or a varray. Operator TABLE informs Oracle that the value is a collection, not a scalar value.


WHERE CURRENT OF cursor_name

Refers to the latest row processed by the FETCH statement associated with the specified cursor. The cursor must be FOR UPDATE and must be open and positioned on a row.

If the cursor is not open, the CURRENT OF clause causes an error. If the cursor is open, but no rows have been fetched or the last fetch returned no rows, PL/SQL raises the predefined exception NO_DATA_FOUND.


WHERE search_condition

Chooses which rows to update in the database table. Only rows that meet the search condition are updated. If you omit this clause, all rows in the table are updated.

Usage Notes

You can use the UPDATE WHERE CURRENT OF statement after a fetch from an open cursor (including fetches done by a cursor FOR loop), provided the associated query is FOR UPDATE. This statement updates the row that was just fetched.

The implicit cursor SQL and the cursor attributes %NOTFOUND, %FOUND, %ROWCOUNT, and %ISOPEN let you access useful information about the execution of an UPDATE statement.

Examples

The following example demonstrates how to update table rows based on conditions, and how to store the updated values, columns, or entire rows in PL/SQL variables:

-- Create some rows with values in all caps like (EMPLOYEES,TABLE)
-- and (EMP_JOB_IX,INDEX).
CREATE TABLE my_objects AS SELECT object_name, object_type FROM user_objects;

DECLARE
   my_name my_objects.object_name%TYPE;
   my_type my_objects.object_type%TYPE;
   TYPE name_typ IS TABLE OF my_objects.object_name%TYPE INDEX BY PLS_INTEGER;
   TYPE type_typ IS TABLE OF my_objects.object_type%TYPE INDEX BY PLS_INTEGER;
   all_names name_typ;
   all_types type_typ;
   TYPE table_typ IS TABLE OF my_objects%ROWTYPE INDEX BY PLS_INTEGER;
   all_rows table_typ;
BEGIN
-- Show the first 10 rows as they originally were.
   FOR obj IN (SELECT * FROM my_objects WHERE ROWNUM < 11)
   LOOP
      dbms_output.put_line('Name = ' || obj.object_name || ', type = ' ||
         obj.object_type);
   END LOOP;
   UPDATE my_objects SET object_name = LOWER(object_name)
       WHERE object_type = 'TABLE';
   dbms_output.put_line('*** Updated ' || SQL%ROWCOUNT || ' rows. ***');
-- Show the first 10 rows after the update.
-- Only some of the names (the table names) have been changed to lowercase.
   FOR obj IN (SELECT * FROM my_objects WHERE ROWNUM < 11)
   LOOP
      dbms_output.put_line('Name = ' || obj.object_name || ', type = ' ||
         obj.object_type);
   END LOOP;

-- Update a single row, and store the values of updated (or unchanged)
-- columns in variables.
   UPDATE my_objects SET object_name = INITCAP(object_name)
      WHERE object_name = 'employees'
      RETURNING object_name, object_type INTO my_name, my_type;

   dbms_output.put_line('*** Updated ' || SQL%ROWCOUNT || ' rows. ***');
   dbms_output.put_line('Affected this row: ' || my_name || ', ' || my_type);

-- Update many rows, storing the values of updated (or unchanged)
-- columns in collections of records. Can't use 'RETURNING *', have
-- to list the columns individually.
   UPDATE my_objects SET object_name = INITCAP(object_name)
      WHERE object_type IN ('TRIGGER','VIEW','SEQUENCE')
      RETURNING object_name, object_type BULK COLLECT INTO all_rows;

   dbms_output.put_line('*** Updated ' || SQL%ROWCOUNT || ' rows. ***');
   FOR i IN all_rows.FIRST .. all_rows.LAST
   LOOP
      dbms_output.put_line('Affected this row: ' || all_rows(i).object_name || ', ' || all_rows(i).object_type);
   END LOOP;

-- Update many rows, storing the values of updated (or unchanged)
-- columns in separate collections. (Generally less useful than using
-- collections of records as above.)
   UPDATE my_objects SET object_name = INITCAP(object_name)
      WHERE object_type IN ('INDEX','PROCEDURE')
      RETURNING object_name, object_type BULK COLLECT INTO all_names, all_types;

   dbms_output.put_line('*** Updated ' || SQL%ROWCOUNT || ' rows. ***');
   FOR i IN all_names.FIRST .. all_names.LAST
   LOOP
      dbms_output.put_line('Affected this row: ' || all_names(i) || ', ' || all_types(i));
   END LOOP;

END;
/

DROP TABLE my_objects;

Related Topics

DELETE Statement, FETCH Statement, INSERT Statement

2025-07-23 09:33:01 2025-07-23 09:33:01.128 -> [main] -> INFO com.fc.V2Application - Starting V2Application v0.0.1-SNAPSHOT using Java 1.8.0_212 on ddad2af35401 with PID 1 (/app.war started by root in /) 2025-07-23 09:33:01 2025-07-23 09:33:01.129 -> [main] -> DEBUG com.fc.V2Application - Running with Spring Boot v2.4.1, Spring v5.3.2 2025-07-23 09:33:01 2025-07-23 09:33:01.129 -> [main] -> INFO com.fc.V2Application - The following profiles are active: dev 2025-07-23 09:33:01 2025-07-23 09:33:01.176 -> [main] -> INFO o.s.b.d.env.DevToolsPropertyDefaultsPostProcessor - For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2025-07-23 09:33:02 2025-07-23 09:33:02.134 -> [main] -> INFO o.s.d.r.config.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode! 2025-07-23 09:33:02 2025-07-23 09:33:02.136 -> [main] -> INFO o.s.d.r.config.RepositoryConfigurationDelegate - Bootstrapping Spring Data Redis repositories in DEFAULT mode. 2025-07-23 09:33:02 2025-07-23 09:33:02.168 -> [main] -> INFO o.s.d.r.config.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 20 ms. Found 0 Redis repository interfaces. 2025-07-23 09:33:02 2025-07-23 09:33:02.747 -> [main] -> INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'removeDruidAdConfig' of type [com.fc.v2.common.druid.RemoveDruidAdConfig$$EnhancerBySpringCGLIB$$c48737ab] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-07-23 09:33:02 2025-07-23 09:33:02.765 -> [main] -> INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'druidStatInterceptor' of type [com.alibaba.druid.support.spring.stat.DruidStatInterceptor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-07-23 09:33:02 2025-07-23 09:33:02.767 -> [main] -> INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'druidStatPointcut' of type [org.springframework.aop.support.JdkRegexpMethodPointcut] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-07-23 09:33:02 2025-07-23 09:33:02.769 -> [main] -> INFO o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'druidStatAdvisor' of type [org.springframework.aop.support.DefaultPointcutAdvisor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2025-07-23 09:33:03 2025-07-23 09:33:03.096 -> [main] -> INFO o.s.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http) 2025-07-23 09:33:03 2025-07-23 09:33:03.110 -> [main] -> INFO org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"] 2025-07-23 09:33:03 2025-07-23 09:33:03.111 -> [main] -> INFO org.apache.catalina.core.StandardService - Starting service [Tomcat] 2025-07-23 09:33:03 2025-07-23 09:33:03.111 -> [main] -> INFO org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.41] 2025-07-23 09:33:04 2025-07-23 09:33:04.184 -> [main] -> INFO o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext 2025-07-23 09:33:04 2025-07-23 09:33:04.185 -> [main] -> INFO o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 3008 ms 2025-07-23 09:33:04 2025-07-23 09:33:04.265 -> [main] -> INFO o.s.boot.web.servlet.RegistrationBean - Filter webStatFilter was not registered (possibly already registered?) 2025-07-23 09:33:04 2025-07-23 09:33:04.298 -> [main] -> DEBUG com.fc.v2.common.conf.PutFilter - Filter 'putFilter' configured for use 2025-07-23 09:33:05 2025-07-23 09:33:05.262 -> [main] -> INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor 2025-07-23 09:33:05 2025-07-23 09:33:05.264 -> [main] -> INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main 2025-07-23 09:33:05 2025-07-23 09:33:05.276 -> [main] -> INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 2025-07-23 09:33:05 2025-07-23 09:33:05.276 -> [main] -> INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.2 created. 2025-07-23 09:33:05 2025-07-23 09:33:05.277 -> [main] -> INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized. 2025-07-23 09:33:05 2025-07-23 09:33:05.278 -> [main] -> INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.2) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED' 2025-07-23 09:33:05 Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. 2025-07-23 09:33:05 NOT STARTED. 2025-07-23 09:33:05 Currently in standby mode. 2025-07-23 09:33:05 Number of jobs executed: 0 2025-07-23 09:33:05 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads. 2025-07-23 09:33:05 Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 2025-07-23 09:33:05 2025-07-23 09:33:05 2025-07-23 09:33:05.278 -> [main] -> INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties' 2025-07-23 09:33:05 2025-07-23 09:33:05.278 -> [main] -> INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.2 2025-07-23 09:33:05 2025-07-23 09:33:05.478 -> [main] -> INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited 2025-07-23 09:33:05 2025-07-23 09:33:05.723 -> [main] -> DEBUG c.f.v.m.auto.SysQuartzJobMapper.selectByExample - ==> Preparing: select id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status from t_sys_quartz_job 2025-07-23 09:33:05 2025-07-23 09:33:05.941 -> [main] -> DEBUG c.f.v.m.auto.SysQuartzJobMapper.selectByExample - ==> Parameters: 2025-07-23 09:33:06 2025-07-23 09:33:06.005 -> [main] -> WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzSchedulerUtil': Invocation of init method failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: 2025-07-23 09:33:06 ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 ### The error may exist in URL [jar:file:/app.war!/WEB-INF/classes!/mybatis/auto/SysQuartzJobMapper.xml] 2025-07-23 09:33:06 ### The error may involve com.fc.v2.mapper.auto.SysQuartzJobMapper.selectByExample-Inline 2025-07-23 09:33:06 ### The error occurred while setting parameters 2025-07-23 09:33:06 ### SQL: select id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status from t_sys_quartz_job 2025-07-23 09:33:06 ### Cause: java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 2025-07-23 09:33:06.005 -> [main] -> INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down. 2025-07-23 09:33:06 2025-07-23 09:33:06.005 -> [main] -> INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused. 2025-07-23 09:33:06 2025-07-23 09:33:06.005 -> [main] -> INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete. 2025-07-23 09:33:06 2025-07-23 09:33:06.006 -> [main] -> INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-1} closing ... 2025-07-23 09:33:06 2025-07-23 09:33:06.014 -> [main] -> INFO com.alibaba.druid.pool.DruidDataSource - {dataSource-1} closed 2025-07-23 09:33:06 2025-07-23 09:33:06.016 -> [main] -> INFO org.apache.catalina.core.StandardService - Stopping service [Tomcat] 2025-07-23 09:33:06 2025-07-23 09:33:06.030 -> [main] -> INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener - 2025-07-23 09:33:06 2025-07-23 09:33:06 Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2025-07-23 09:33:06 2025-07-23 09:33:06.047 -> [main] -> ERROR org.springframework.boot.SpringApplication - Application run failed 2025-07-23 09:33:06 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'quartzSchedulerUtil': Invocation of init method failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: 2025-07-23 09:33:06 ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 ### The error may exist in URL [jar:file:/app.war!/WEB-INF/classes!/mybatis/auto/SysQuartzJobMapper.xml] 2025-07-23 09:33:06 ### The error may involve com.fc.v2.mapper.auto.SysQuartzJobMapper.selectByExample-Inline 2025-07-23 09:33:06 ### The error occurred while setting parameters 2025-07-23 09:33:06 ### SQL: select id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status from t_sys_quartz_job 2025-07-23 09:33:06 ### Cause: java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) 2025-07-23 09:33:06 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:429) 2025-07-23 09:33:06 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1780) 2025-07-23 09:33:06 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:609) 2025-07-23 09:33:06 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:531) 2025-07-23 09:33:06 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) 2025-07-23 09:33:06 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) 2025-07-23 09:33:06 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) 2025-07-23 09:33:06 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) 2025-07-23 09:33:06 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) 2025-07-23 09:33:06 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:923) 2025-07-23 09:33:06 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:588) 2025-07-23 09:33:06 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) 2025-07-23 09:33:06 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) 2025-07-23 09:33:06 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) 2025-07-23 09:33:06 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) 2025-07-23 09:33:06 at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) 2025-07-23 09:33:06 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) 2025-07-23 09:33:06 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) 2025-07-23 09:33:06 at com.fc.V2Application.main(V2Application.java:12) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 2025-07-23 09:33:06 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 2025-07-23 09:33:06 at java.lang.reflect.Method.invoke(Method.java:498) 2025-07-23 09:33:06 at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) 2025-07-23 09:33:06 at org.springframework.boot.loader.Launcher.launch(Launcher.java:107) 2025-07-23 09:33:06 at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) 2025-07-23 09:33:06 at org.springframework.boot.loader.WarLauncher.main(WarLauncher.java:59) 2025-07-23 09:33:06 Caused by: org.springframework.jdbc.BadSqlGrammarException: 2025-07-23 09:33:06 ### Error querying database. Cause: java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 ### The error may exist in URL [jar:file:/app.war!/WEB-INF/classes!/mybatis/auto/SysQuartzJobMapper.xml] 2025-07-23 09:33:06 ### The error may involve com.fc.v2.mapper.auto.SysQuartzJobMapper.selectByExample-Inline 2025-07-23 09:33:06 ### The error occurred while setting parameters 2025-07-23 09:33:06 ### SQL: select id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent, status from t_sys_quartz_job 2025-07-23 09:33:06 ### Cause: java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239) 2025-07-23 09:33:06 at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70) 2025-07-23 09:33:06 at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73) 2025-07-23 09:33:06 at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) 2025-07-23 09:33:06 at com.sun.proxy.$Proxy86.selectList(Unknown Source) 2025-07-23 09:33:06 at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) 2025-07-23 09:33:06 at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139) 2025-07-23 09:33:06 at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76) 2025-07-23 09:33:06 at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) 2025-07-23 09:33:06 at com.sun.proxy.$Proxy114.selectByExample(Unknown Source) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 2025-07-23 09:33:06 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 2025-07-23 09:33:06 at java.lang.reflect.Method.invoke(Method.java:498) 2025-07-23 09:33:06 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) 2025-07-23 09:33:06 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) 2025-07-23 09:33:06 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) 2025-07-23 09:33:06 at com.alibaba.druid.support.spring.stat.DruidStatInterceptor.invoke(DruidStatInterceptor.java:73) 2025-07-23 09:33:06 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) 2025-07-23 09:33:06 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) 2025-07-23 09:33:06 at com.sun.proxy.$Proxy115.selectByExample(Unknown Source) 2025-07-23 09:33:06 at com.fc.v2.service.SysQuartzJobService.selectByExample(SysQuartzJobService.java:108) 2025-07-23 09:33:06 at com.fc.v2.service.SysQuartzJobService$$FastClassBySpringCGLIB$$8fc47ef9.invoke(<generated>) 2025-07-23 09:33:06 at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) 2025-07-23 09:33:06 at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:687) 2025-07-23 09:33:06 at com.fc.v2.service.SysQuartzJobService$$EnhancerBySpringCGLIB$$b174130e.selectByExample(<generated>) 2025-07-23 09:33:06 at com.fc.v2.common.quartz.QuartzSchedulerUtil.init(QuartzSchedulerUtil.java:42) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 2025-07-23 09:33:06 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 2025-07-23 09:33:06 at java.lang.reflect.Method.invoke(Method.java:498) 2025-07-23 09:33:06 at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) 2025-07-23 09:33:06 at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) 2025-07-23 09:33:06 at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) 2025-07-23 09:33:06 ... 27 common frames omitted 2025-07-23 09:33:06 Caused by: java.sql.SQLSyntaxErrorException: Table 'springbootv2.t_sys_quartz_job' doesn't exist 2025-07-23 09:33:06 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) 2025-07-23 09:33:06 at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) 2025-07-23 09:33:06 at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) 2025-07-23 09:33:06 at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) 2025-07-23 09:33:06 at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370) 2025-07-23 09:33:06 at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3461) 2025-07-23 09:33:06 at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440) 2025-07-23 09:33:06 at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3459) 2025-07-23 09:33:06 at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167) 2025-07-23 09:33:06 at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:497) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 2025-07-23 09:33:06 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 2025-07-23 09:33:06 at java.lang.reflect.Method.invoke(Method.java:498) 2025-07-23 09:33:06 at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59) 2025-07-23 09:33:06 at com.sun.proxy.$Proxy118.execute(Unknown Source) 2025-07-23 09:33:06 at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63) 2025-07-23 09:33:06 at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79) 2025-07-23 09:33:06 at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63) 2025-07-23 09:33:06 at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:326) 2025-07-23 09:33:06 at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) 2025-07-23 09:33:06 at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) 2025-07-23 09:33:06 at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83) 2025-07-23 09:33:06 at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) 2025-07-23 09:33:06 at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 2025-07-23 09:33:06 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 2025-07-23 09:33:06 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 2025-07-23 09:33:06 at java.lang.reflect.Method.invoke(Method.java:498) 2025-07-23 09:33:06 at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) 2025-07-23 09:33:06 ... 57 common frames omitted
最新发布
07-24
<think>我们正在处理一个Spring Boot启动失败的问题,错误信息是数据库表不存在:Table 'springbootv2.t_sys_quartz_job' doesn't exist。 这个错误通常发生在应用程序启动时,尝试访问数据库中的表,但该表不存在。 可能的原因和解决方案: 1. 数据库表确实没有创建:检查数据库,确认表't_sys_quartz_job'是否存在。如果不存在,需要创建该表。 - 如果是使用JPA(Hibernate)自动创建表,检查配置: spring.jpa.hibernate.ddl-auto=update 确保这个配置是开启的,这样在启动时如果表不存在,Hibernate会自动创建。 但是注意:如果表结构发生变化,update可以更新表,但不会删除列。如果需要完全从实体创建表,可以使用create(但会删除现有表)或create-drop(应用关闭时删除表)。 2. 数据库连接配置错误:检查数据库连接URL、用户名和密码是否正确,确保连接的是正确的数据库(这里是springbootv2)。 3. SQL脚本未执行:如果项目中有使用SQL初始化脚本(如schema.sql或data.sql),检查这些脚本是否正确,特别是表名是否正确(注意大小写,有些数据库区分大小写,比如Linux下的MySQL默认区分,而Windows不区分)。 - 注意:如果使用了JPA的自动创建,同时也有schema.sql,那么默认情况下,schema.sql会在Hibernate自动创建之后执行,这可能导致冲突。可以通过配置: spring.jpa.defer-datasource-initialization=true 这样就会先执行Hibernate的DDL,然后再执行schema.sql和data.sql。 4. 实体类与表名映射问题:检查与表't_sys_quartz_job'对应的实体类,是否使用了正确的表名注解。 - 例如:@Table(name = "t_sys_quartz_job") - 注意:如果表名在数据库中是区分大小写的,而数据库中实际表名是其他形式(如全大写或全小写),需要确保注解中的表名与数据库中的一致。 5. 数据库权限问题:检查连接数据库的用户是否有创建表或访问该表的权限。 6. 数据库版本问题:如果表结构使用了新的数据库特性,而当前数据库版本不支持,也会导致创建失败。 7. 多数据源配置问题:如果项目中配置了多个数据源,可能这个表不在默认数据源中,或者数据源配置不正确导致表创建在了错误的数据库。 8. 使用Flyway或Liquibase:如果项目使用了数据库版本管理工具(如Flyway或Liquibase),检查迁移脚本是否正确,以及是否已经成功执行。 步骤: 1. 首先,检查数据库springbootv2中是否存在表t_sys_quartz_job。可以直接登录数据库执行: SHOW TABLES LIKE 't_sys_quartz_job'; 如果没有,则查看日志中是否有创建表的语句,以及是否有错误。 2. 检查Spring Boot的配置,特别是数据源和JPA的配置。 3. 查看启动日志,搜索与表创建相关的SQL语句,看是否有错误信息。 4. 如果使用了数据库迁移工具,检查迁移脚本。 5. 如果表确实需要由应用程序自动创建,确保配置正确。 6. 如果表应该由初始化脚本创建,检查脚本的位置和内容。 7. 考虑是否在实体类中使用了错误的表名。 由于错误信息明确指出了表不存在,因此重点放在表的创建上。 示例配置(application.properties): spring.datasource.url=jdbc:mysql://localhost:3306/springbootv2?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.jpa.hibernate.ddl-auto=update # 如果需要先执行schema.sql,可以设置 spring.sql.init.mode=always # Spring Boot 2.5+ 使用这个,之前版本是spring.datasource.initialization-mode=always spring.jpa.defer-datasource-initialization=true # 如果同时使用Hibernate DDL和脚本 注意:在Spring Boot 2.5及以上版本,初始化脚本的执行方式有所变化。 如果问题仍然存在,可以尝试手动创建表,然后启动应用,看是否还有其他错误。 另外,检查实体类中关于定时任务作业的映射是否正确,特别是字段映射。 如果表名在数据库中是区分大小写的,而代码中表名使用了不同的情况,也可能导致问题。例如,数据库中的表名可能是大写,而代码中是小写,这时需要统一(或者使用引号括起来,但一般不推荐)。 在MySQL中,可以通过设置lower_case_table_names来配置是否区分大小写。如果设置为0,则区分大小写;1表示不区分(表名以小写存储);2(不区分大小写,但以大小写混合存储)。建议在开发中统一使用小写表名以避免问题。 总结:先检查数据库是否存在该表,然后检查配置,最后检查实体映射和初始化脚本。</think>### Spring Boot启动失败:数据库表不存在解决方案 当Spring Boot应用启动失败并报错`Table 'springbootv2.t_sys_quartz_job' doesn't exist`时,表明应用尝试访问数据库中的`t_sys_quartz_job`表但未找到。以下是系统性的排查步骤和解决方案: --- ### 一、核心原因分析 1. **表未创建** - 数据库迁移未执行(如Flyway/Liquibase脚本未运行) - JPA自动建表配置未启用或失效 - 手动执行的SQL脚本遗漏该表 2. **数据源配置错误** - 连接了错误的数据库(如`springbootv2`库不存在) - 多数据源配置冲突导致建表在错误的库[^1] 3. **命名问题** - 表名大小写敏感(MySQL在Linux下默认区分大小写) - 实体类`@Table(name="t_sys_quartz_job")`注解与实际表名不一致 4. **初始化顺序问题** - Quartz定时任务在表创建前初始化(需延迟Quartz启动) --- ### 二、解决方案步骤 #### ✅ 1. **验证数据库状态** ```sql -- 检查数据库是否存在 SHOW DATABASES LIKE 'springbootv2'; -- 检查目标表是否存在 USE springbootv2; SHOW TABLES LIKE 't_sys_quartz_job'; ``` - **若库/表不存在**:手动创建或修复迁移脚本 #### ✅ 2. **启用JPA自动建表** 在`application.properties`中添加: ```properties # 自动创建/更新表结构 spring.jpa.hibernate.ddl-auto=update # 显示执行的SQL语句(调试用) spring.jpa.show-sql=true ``` > **注意**:生产环境建议使用`validate`而非`update`,需通过迁移脚本管理表结构[^2]。 #### ✅ 3. **检查数据源配置** ```properties # 确认连接的是正确数据库 spring.datasource.url=jdbc:mysql://host:3306/springbootv2 spring.datasource.username=your_user spring.datasource.password=your_password # 多数据源场景需指定Quartz的库 spring.quartz.jdbc.initialize-schema=always ``` #### ✅ 4. **处理表名大小写敏感问题** - **方案1**:统一表名为小写(推荐) ```java @Entity @Table(name = "t_sys_quartz_job") // 显式指定小写表名 public class QuartzJob { ... } ``` - **方案2**:修改MySQL配置(`my.cnf`): ```ini [mysqld] lower_case_table_names=1 ``` #### ✅ 5. **延迟Quartz初始化** 在`application.properties`中添加: ```properties # 确保数据库初始化完成后再启动Quartz spring.quartz.wait-for-jobs-to-complete-on-shutdown=true spring.quartz.jdbc.initialize-schema=never ``` #### ✅ 6. **验证SQL初始化脚本** - 检查`schema.sql`或`data.sql`是否包含建表语句: ```sql CREATE TABLE IF NOT EXISTS t_sys_quartz_job (...); ``` - Spring Boot 2.5+需显式启用初始化: ```properties spring.sql.init.mode=always ``` --- ### 三、预防措施 1. **版本控制数据库** 使用Flyway/Liquibase管理迁移脚本,避免手动操作失误。 ```xml <!-- Flyway依赖 --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> ``` 2. **测试环境隔离** 为开发/测试环境配置独立数据库,避免与生产环境冲突。 3. **启动顺序控制** 通过`@DependsOn`注解确保Bean初始化顺序: ```java @Bean @DependsOn("liquibase") // 确保先执行数据库迁移 public SchedulerFactoryBean schedulerFactoryBean() { ... } ``` --- ### 四、排查工具建议 - **日志分析**:搜索`Creating table`或`t_sys_quartz_job`关键词,确认建表SQL是否执行。 - **调试模式**:添加`debug=true`查看Bean初始化顺序。 - **数据库监控**:使用Druid连接池监控SQL执行情况。 > 通过上述步骤,95%以上的表不存在问题可被解决。若仍存在异常,需检查数据库用户权限或网络连接问题[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值