How to extract Primary Key Strings in Oracle

Source: Internet
Author: User
Recently, I was working on an Oracle project for a database. It was also my first project module. It felt a lot of pain, but there were a lot of GAINS. The following are my problems and solutions.

Recently, I was working on an Oracle project for a database. It was also my first project module. It felt a lot of pain, but there were a lot of GAINS. The following are my problems and solutions.

Recently, I was working on an Oracle project for a database. It was also my first project module. It felt a lot of pain, but there were a lot of GAINS. The following are my problems and solutions. I am a cainiao-level expert. If you find any errors, please point them out.

For example:

The primary key of the created Oracle table: "four-digit tree code"

For example, in the User table:

Table Name: USER

Table record:

USER_ID CHAR (15) not null,

USER_NAME VARCHAR2 (20) not null,

USER_EMAIL VARCHAR2 (30 ),

Primary key (USER_ID)

Insert a data entry first:

Insert into user values ('20170101', 'zhang san', '2017 @');

Now I want to query:

SELECT * from user;

Execution result:

USER_ID USER_NAME USER_EMAIL

0001 Zhang San 123456789 @

If we only query USER_ID from Oracle

SELECT USER_ID

From user;

Execution result:

USER_ID

0001

However, when we spell the string SQL, for example, we want to join the Table query, USER_ID is an inline field in another table, we need to use it in the USER_ID inline table.

For example, in the "Student table:

Table Name: STUDENT

Table record:

STUDENT_ID CHAR (15) not null,

USER_ID CHAR (15) not null,

STUDENT_NAME VARCHAR2 (20) not null,

Primary key (STUDENT_ID)

Insert a record:

Insert into student values ('20170101', '20160301', 'student Michael ');

Query (here, I want to allocate email information to students)

Select s. STUDENT_NAME, U. USER_MAIL

From student s, USER U

Where s. USER_ID = U. USER_ID;

Execution result:

STUDENT_NAME USERMAIL

Student Zhang San 123456789 @ 163..com

Important:

In the database, it is correct to use this method directly. If we use string SQL in the java file to spell strings, the problem will occur.

Use JDBC connection to execute SQL statements and return query results. The JDBC processing is omitted here.

Assume that the public List BaseListSql (String SQL); Method Processing.

SQL spelling method:

(1)

// In this sentence, user_name is the input parameter. The value of user_id is found based on the name of the input user.

String usr_id = this. baseListSql ("SELECT USER_ID from user where USER_NAME = '" + user_name + "'");

// Associate USER_MAIL in the USER table based on user_id

String SQL = "SELECT S. STUDENT_NAME, U. USER_MAIL FROM STUDENT S, USER U WHERE S. USER_ID = '" + user_id + "'";

// Return List after JDBC execution

List Students = this. baseListSql (SQL );

// Return data

Return students;

Analysis:

When you look at it directly, you still think that the SQL logic is correct and the strings are spelled out correctly, but the execution is not successful. Of course, you can use System. out. println (SQL); and Debug mode for debugging, but I want to write it down here. I don't want to Debug it for half a day every time because of this error.

Here, USER_ID is of the CHAR type and the length is 15. We use the "four-digit Tree Code" to input '123456' as the data so that when it is found to be '1234568', it will be followed by all spaces, therefore, the input values in the SQL statements below are incorrect, so some processing is required, such as solution (2)

(2)

// In this sentence, user_name is the input parameter. The value of user_id is found based on the name of the input user.

String usr_id = this. baseListSql ("SELECT USER_ID from user where USER_NAME = '" + user_name + "'");

// First, use the trim () method of String to remove all spaces. This is correct.

User_id = user_id.trim ();

// Associate USER_MAIL in the USER table based on user_id

String SQL = "SELECT S. STUDENT_NAME, U. USER_MAIL FROM STUDENT S, USER U WHERE S. USER_ID = '" + user_id + "'";

// Return List after JDBC execution

List Students = this. baseListSql (SQL );

// Return data

Return students;

There may be errors in the example, and I hope to find the error and point it out.
In this way, the execution is successful! I did not pay attention when I was looking for it. Fortunately, I had a hard time. record it and pass it!

Next time you have time to record problems, haha! Reward yourself! If you encounter more problems, you must become a master.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.