Java:oracle ()

Source: Internet
Author: User

--MAX,MIN,SUM,AVG,COUNT,NVL (,)
--Aggregate function
--Max: Max value
--Max can take the maximum value of the number, or the maximum value of the string (alphabetical order), according to the actual meaning of the scene, it is best to appear in a SQL only once
Select Max (age), Max (name) from teacher;
--min: Minimum value
Select min (age) from teacher;
Select min (name) from teacher;
--sum: sum: In the SUM function, only numeric types can be computed, and string types cannot be evaluated
Select SUM (age) from teacher;
Select SUM (name) from teacher;
--avg: Average: Can also be used to calculate numeric types
Select AVG (age) from teacher;
Select AVG (name) from teacher;
--count (*), COUNT (any number): Gets the total number of data bars for a table
--In the actual development position, be sure to use COUNT (1) Do not use COUNT (*)
Select COUNT (*) counts from teacher;
The efficiency of Count (*) and select * is very similar: first, all the data is checked out (call select * from XXX), then the total number of statistics
Select COUNT (1) counts from teacher;
--COUNT (1): First change all the data in the table to 1, and then the total number of statistics
--NVL: If the queried data has a null value, give a default value
Select name, age, NVL (description, ' I am the default ') from teacher;

--Multi-table query
--Start by querying all data tables----> Begin a table with a small amount of data, and then group the tables with less data
SELECT * from Classes;
SELECT * from teacher;
SELECT * from teacher, classes;
--take classes as the benchmark, query the number of times: Classes How many data, you will query how many times
--in real-world development, this multi-table query from XXX,XXX,XX is not used because it is very inefficient
Select C.*, t.* from classes C, teacher T;
Select T.id teahcer_id, T.description teacher_description, T.age teacher_age, T.name teahcer_name, T.hire_date teacher_ Hire_date, C.id classes_id, C.name classes_name, c.description classes_description from classes C, teacher T;
--In a multi-table query, once an alias is given to the table, the alias must be used for the Point column name (the alias point column name is used when only two tables exist in the same field)
--there is no primary key in the dictionary table
Select C.*, t.* from classes C, teacher t where age = 33;
--Query out two data contents with the same table ID
Select C.*, t.* from classes C, teacher t where c.id = t.id;
--bubble sort: Take the small and press it down.
--Oracle also has its own collation, similar to the bubbling sort
Select C.*, t.* from classes C, teacher T where c.id > t.id;
Select C.name, t.name from classes C, teacher T Group by C.name, T.name;
SELECT * from teacher;
SELECT * from Classes;

--one to one, one to many, many to many
--There must be a connection between these two tables or more than one table.

-One on one
--Step One: Build two tables
--Second step: Find the relationship between the two tables (one-to-many, or many-to-many)
--to discover a one-to-one relationship between native origin and people
--Step Three: Create a foreign key (the column that is associated with another), you must point to the primary key of another associated table, and the column type of the foreign key must be consistent with the type of the column that points to the primary key
--Two tables of a relationship, foreign keys can be built at will, in other words, the foreign key is established in which table is not related
--person table, ID primary key, native_place_id foreign key (the column type of the foreign key must be the same as the primary key ID type (length) in the Native_place table)
--native_place table, ID primary key
Select P.name, Np.area from Person P, native_place np where p.native_place_id = np.id;

--one-to-many
--First step: Build two Tables
--Employee/staff Staff table
--Department Department table
--The second step: look for the relationship between the two tables (one-to-many, the department is a party, the employee is a multi-party)
--Step Three: Create a foreign key
--The foreign key needs to be built on one side: in the case of departments and staff tables: A bug occurs: A department can only have one employee, although it does not violate the grammar, but does not conform to logical thinking
--In a one-to-many case, the foreign key should be built on a multi-party
--If I need to find out which department all the employees belong to
--A Department of staff:
--SELECT * from Department D, the staff s where d.staff_id = S.id;
--Query All employees:
SELECT * FROM Department D, the staff s where d.id = s.department_id;
--Find out the number of employees in each department
--First step: Find out the Department
--The second step: to find out the related table of the Department
--Step three: need to group departments
Select D.name, COUNT (1) from Department D, the staff s where d.id = s.department_id GROUP by D.name;

--many-to-many
--Students and courses
--first set up two sheets
--analysis of the relationship between the two tables (many-to-many)
--Create a foreign key: the foreign key is not suitable for which table is built
--Create a foreign key: redefine a table to manage the foreign key relationships inside
--The table for controlling course selection is in the Third foreign key table
--Take the student table and curriculum as an example:
--The first step: put all the students into the student table (do not know what course students choose)
--Step Two: Put all the courses into the curriculum (you don't know which students will choose the courses)
--Step Three: a many-to-many relationship table (determines the outcome of the selected course): Determine which students have chosen the course, and which students have chosen
--many-to-many relationship tables (insure): There are only two fields in it, and there are no primary keys, each of which represents the primary key for two tables (student_id---id,class_id of the >stu_students table----->stu_ The primary key ID of the class table)
SELECT * FROM Stu_students SST, Stu_class SC, insure I where i.student_id = sst.id and i.class_id = sc.id;

Java:oracle ()

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.