Https://www.2cto.com/database/201710/688377.html
1, the role of triggers?
A trigger is a special stored procedure that is executed primarily by triggering an event. It can enforce constraints to maintain the integrity and consistency of data, and can track operations within the database without permitting unauthorized updates and changes. Can be associated with a level operation. For example, a trigger on a table contains data operations on another table, and that action causes the table trigger to be triggered.
2. What is a stored procedure? Using what to invoke?
A stored procedure is a precompiled SQL statement, and the advantage is that it allows for a modular design, meaning that it is created once and can be called more than once in the program. If an operation needs to execute multiple times of SQL, using a stored procedure is faster than simply SQL statement execution. Call: 1) You can invoke a stored procedure with a command object. 2) can be called by external programs, such as: Java programs.
3. What are the advantages and disadvantages of stored procedures?
Pros: 1 The stored procedure is pre-compiled, and the execution efficiency is high. 2) The code of the stored procedure is stored directly in the database and is directly called through the stored procedure name to reduce network traffic. 3) security is high, the execution of stored procedures requires a user with certain permissions. 4) Stored procedures can be reused to reduce the workload of database developers. Cons: Poor portability
4. The difference between stored procedure and function
| Stored Procedures |
Function |
| Used to complete specific operations or tasks (such as inserting, deleting, etc.) in the database |
For specific data (for example, selection) |
| Program Head declaration with procedure |
Program Head declaration with function |
| The program header declaration does not need to describe the return type |
The return type is described when the program header is declared, and at least one valid return statement is included in the PL/SQL block |
| Parameters can be used for in/out/in out three modes |
Parameters can be used for in/out/in out three modes |
| Can be executed as a standalone PL/SQL statement |
cannot be executed independently and must be called as part of an expression |
| 0 or more values can be returned by out/in out |
Returns a value from the return statement, either in the same order as the declaration part, or as a variable out of an out type parameter |
| A stored procedure is not callable in an SQL statement (DML or select) |
A function can be called in a SQL statement (DML or select) |
5, the role of the index? And what are the pros and cons of it?
The index is a special query table, the database search can use it to speed up the retrieval of data. It is similar to the catalogue of real-life books, and you can find the data you want without having to query the entire contents of the book. Indexes can be unique, and creating an index allows you to specify a single column or multiple columns. The disadvantage is that it slows down data entry and also increases the size of the database.
6. What kind of field is suitable for indexing
A unique, non-empty, frequently queried field
7. What are the index types?
Logically:
Single Column row index
Concatenated multi-line Index
Unique Unique index
Nonunique Non-Unique index
function-based Function Index
Domain index is physically:
Partitioned Partition Index
nonpartitioned Non-partitioned indexes
B-tree:
Normal B-Tree
Rever Key Anti-transformation B-Tree Bitmap bitmap Index
8. What is a transaction? What is a lock?
A transaction is a grouping of SQL statements that are bound together as a logical unit of work, and if any one statement fails, the entire operation is failed, and later the operation is rolled back to the pre-operation state, or there is a node on it. A transaction can be used to ensure that it is either executed or not executed. To consider a set of statements as transactions, you need to pass acid testing, that is, atomicity, consistency, isolation, and persistence. Lock: In so the DBMS, the lock is the key to implement the transaction, the lock can guarantee the integrity and concurrency of the transaction. Like a real-life lock, it enables certain data owners to be unable to use certain data or structures for a certain period of time. Of course, locks are also divided into levels.
9. What is a view? What is a cursor?
View: is a virtual table that has the same functionality as a physical table. You can add, change, check, manipulate, and attempt a view that is usually a subset of rows or columns that have a table or multiple tables. Changes to the view affect the base table. It makes it easier for us to get data, compared to multiple table queries. Cursor: is a query out of the result set as a unit to effectively handle. A cursor can be set to a specific row in that cell, retrieving one or more rows from the current row of the result set. You can make modifications to the current row of the result set. Cursors are generally not used, but cursors are important when you need to process the data individually.
10. Advantages and disadvantages of view
Pros: 1) access to the database, because the view can have a selective selection of the part of the database. 2) Users can get results from complex queries through simple queries. 3) Maintain the independence of data and try to retrieve data from multiple tables. 4) Different views can be generated for the same data. Cons: Performance: When querying a view, you must turn the view's query into a query on the base table, and if the view is defined by a complex multi-table query, then the data cannot be changed
11, list several kinds of table connection way, what is the difference?
Internal connection, self-connect, outer join (left, right, full), cross connect within: Only two elements of the table match to be displayed in the result set. Outer connection: Left Outer connection: Driver table on left side, data of driver table is all displayed, matching table mismatch will not be displayed. Right outer connection: the right side is the driver table, the data of the driver table is all displayed, the matching table mismatch will not be displayed. Full outer joins: the unmatched data in the connected tables is all displayed. Cross join: The Cartesian effect, the result of which is the product of the number of linked tables.
12. What is the difference between a primary key and a foreign key?
The primary key is unique in this table, it cannot be null, the foreign key can be duplicated, the foreign key is associated with the primary key of the other table, and a foreign key that does not exist in the corresponding table cannot be created.
13, in the database query statement speed is very slow, how to optimize?
1. Build an index 2. Reduce the association between Tables 3. Optimize SQL, try to get SQL to locate data quickly, do not let SQL do full table query, should go index, the data volume of the table in front of 4. Simplify the query field, do not use the field, you have control of the returned results, and return a small amount of data 5. Try to use PreparedStatement to query, do not use statement
14. What is the three-paradigm of the database?
First paradigm: Columns cannot be divided into second paradigm: rows can be uniquely differentiated, primary key constraints are the third paradigm: The table's non-primary attribute cannot depend on the foreign key constraint of the non-principal property of the other table and the three paradigms are first-class dependent, the second paradigm is based on the primary paradigm, and the third paradigm establishes the first second paradigm
15. What is the difference between Union and union all?
The Union will filter out duplicate records after the table link is made, so the resulting set of results will be sorted after the table is connected, the duplicate records are deleted and the results returned. Most of the actual applications do not produce duplicate records, the most common being the process table and the History table Union. UNION all simply merges two results and returns. Thus, if there are duplicate data in the two result sets returned, the returned result set will contain duplicate data. In terms of efficiency, union All is much faster than union, so if you can confirm that the combined two result sets do not contain duplicate data, then use UNION ALL.
16. What is the difference between Varchar2 and varchar?
The length of the char is fixed, and the length of the VARCHAR2 is changeable, for example, storing the string "abc" for char (20), which means that the characters you store will account for 20 bytes, contain 17 empty, and the same varchar2 (20) accounts for only 3 bytes and 20 is the maximum value. When you store characters that are less than 20 o'clock, store them at the actual length. The efficiency of char is varchar2 by high efficiency. Currently varchar is synonymous with VARCHAR2, the industry standard varchar type can store an empty string, but Oracle cannot do so, although it retains the right to do so later. Oracle has developed a data type of VARCHAR2, which is not a standard varchar, and he will change the property of a varchar column in the database to store null values, and if you want to have backward compatibility capabilities, Oracle recommends using VARCHAR2 instead of varchar
17. What is the difference between Oracle and MySQL?
1) library functions are different. 2) Oracle is managed with a tablespace, not MySQL. 3) Show all current tables, users, change the connection user, display the current connection user, the execution of the external script statement is different. 4) Paging query time, MySQL with limit Oracle with RowNum
- Mysql>SELECT* fromTableLIMIT 5, 10; Retrieving record lines 6-15//To retrieve all record rows from an offset to the end of the recordset, you can specify a second parameter of -1:mysql>SELECT* fromTableLIMIT 95,-1; Retrieving record lines 96- Last. If only one parameter is given, it represents the maximum number of record rows returned: mysql>SELECT* fromTableLIMIT 5; Retrieving the first 5 record lines//In other words, limit n is equivalent to limit 0,n. 5) The syntax of SQL is different.
18. How many types of Oracle statements have Oracle statements divided into three categories: DDL, DML, DCL. DDL (data definition Language) data definition language, including: Create statement: Some objects that can create databases and databases. Drop statement: You can delete data tables, indexes, triggers, conditional constraints, and permissions for data tables. Alter statement: Modifies the data table definition and properties. Truncate statement: Deletes all records in the table, including all spatial allocation records. The DML (data manipulation Language) data manipulation language, which includes: INSERT statement: Inserts a record into the data sheet. Delete statement: Deletes one or more records from a data table, or deletes all records in the datasheet, but its operands are still records. UPDATE statement: Used to modify the contents of a record in an existing table. DCL (Data Control Language) database control language, including: Grant statement: Allows the creator of an object to give certain permissions to a user or to a group or all users (public). Revoke statement: Can revoke a user or a group or all user access rights 19, Oracle paging query statements using RowNum, two of the following: the first: SELECT * FROM (select T.*,rownum row_num from MyTable t) b where B.row_num between 1 and 10 the second type: SELECT * FROM (select A.*, rownum rn from MyTable a where rownum <=) where RN >= 1 uses ROWID as follows: SELECT * from Scott.emp where rowid in (select Rd from (select rowID as Rd, rownum as RN from Scot T.EMP) where rn<=6 and rn>3) 20, randomly fetch 50 SELECT * from the database (SELECT * from T_example ORDER by Dbms_random.random) where RowNum <= 5021, order by and Group BY, order by sort query, ASC ascending, desc descending group by group query, HaviNg can only be used for GROUP BY clauses, within groups, and having conditional clauses that can be directly followed by function expressions. Query statements that use the GROUP BY clause need to use aggregate functions. 22, where the commit will use Oracle's commit is the DML statement submission data (this is the release lock is not a lock table), before the commit before you update the memory, not updated to the physical file. Execution commit from the user's point of view is updated to the physical file, in fact, the commit did not write the date file, but the record redo log file, to write from memory to the data physical file, need to trigger checkpoint, by DBWR this background process to write, here a bit more content , if you don't delve into it, you'll understand that a commit is an update from memory to a physical file. 23, row to column, column wrapping how to turn 1) using the Decode function 2) using CASE when statement 24, what is PL/SQL? PL/SQL is a programming language called the procedural SQL language (procedural language/sql). PL/SQL is an extension of the Oracle database to the statement. In the use of ordinary SQL statements to add the characteristics of the programming language, so PL-SQL to the data Operations and query statements organized in the PL/SQL code of the process unit, through logical judgment, loop and other operations to achieve complex functions or calculations. PL/SQL is available only for Oracle databases. MySQL currently does not support PL/SQL. 25. Sequence of functions Oracle uses sequences to generate unique numbers to handle the self-increment fields in a table. The Oracle sequence is an atomic object and is consistent. That is, once you access a serial number, Oracle automatically increments the next number before processing the next request, ensuring that duplicate values are not present. 26. A relational view of tables and views is actually a query SQL statement that displays related data in one or more tables or other views. A table is the actual storage of data in a relational database. 27, Oracle Basic data type 1) string type char, nchar, varchar, VARCHAR2, NVARCHAR2 2) numeric type number, integer 3) floating-point type Binary_float, Binary_ Double, float 4) Date type day, timestamp 5) LOB type blob, CLOB, NCLOB, BFILE28, truncate and delete differ truncate TABLE functionally with DE with no WHERE clause The lete statement is the same: both delete all rows in the table. But TrunCATE TABLE is faster than DELETE and uses less system and transaction log resources. The DELETE statement deletes one row at a time and records an entry in the transaction log for each row that is deleted.
TRUNCATE table deletes data by releasing the data page used to store the table data, and records the release of the page only in the transaction log. Truncate,delete,drop to compare:
TRUNCATE TABLE: Delete content, free space, but do not delete definition.
Delete TABLE: Deletes the definition without deleting the content and does not free up space.
Drop TABLE: Removes content and definitions and frees up space. 29, Oracle Acquisition system time Select To_char (sysdate, ' Yyyy-mm-dd HH24:mi:ss ') from Dual;30, Oracle How to remove the re-use of the DISTINCT keyword 31, now has a table: A (ID, name,regdate) B (id,groupid) C (id,name2) write out the following SQL statement a) count the number of registered users per month in table a select count (*), To_char (regdate, ' yyyymm ') from a Group BY To_char (RegDate, ' yyyymm '); B) count the number of users with the same name in table a select COUNT (*) from (select name from A group by name has COUNT (*) >1); c) If Table A has the same name as the user, put the same isolated, write to table C nsert into C (name2) select name from A group by name have Count (*) >1; D) The ID in a has multiple identical data, the ID of the same name in a only retains the data with the largest registration time delete from E where E.regdate < (select Max (regdate) from a X where e.id = x.id); 3 2. Existing table: Student (S#,SN,SD) Student table Course (c#,cn,t#) Schedule SC (S#,c#,score) Table 1. Check the student's number and name for the course ' tax '. A: Select SN,SD from Student where s# in (select s# from Course C, SC where C.C#=SC. C # and cn= ' tax base '); 2. Search for students with more than 5 courses number and name answer: Select Sn,sd from Student where s# in (select s# from SC GROUP by s# have count (Distinct C #) &G T 5); 3. Establishment of a student table students, including Name,age,head_teacher,id,score (name, age, class teacher, school number, score) Create table students (Id No. (9) NOT null primary key, Name VARCHAR2 (+) not NULL, age int Check (age between 0 and), Head_teacher vachar2 (+), score float); 4. Insert a record on the above table, name: Zhang San, age: 18, class Teacher: John Doe, School number: INSERT into student (Id,name,age,head_teacher) VALUES (' 22 ', ' Zhang San ', ' 18 ', ' John Doe '); 5. Create an index on the age+name in the table above and explain how it works and how to use the CREATE INDEX Student_index on students (Age,name); 33, how to put such a table (table name: Tmp_table_ 201307) Year Month amount 2012 1 1.1 2012 2 1.2 2012 3 1.3 2012 4 1.4 2012 4 1.6 2013 1 2.1 2013 2 2.2 2013 2 2.2 2013 3 2. 3 2013 4 2.4 to find such a result year M1 M2 M3 M4-1.1 1.2 1.3 3 2.1 4.4 2.3 2.4 Select year, Sum (case when M Onth = 1 Then amount else 0 end) as M1, sum (case if month = 2 then amount else 0 end) as M2, sum (case when month = 3 Then amount else 0 end) as M3, Sum (case is month = 4 then amount else 0 end) as M4 from tmp_table_201307 a Group By the year Order by 1;34, the database has a table ismg_icp_flow, which is structured as follows sql> desc ismg_icp_flow Name Null Type ———————————————————————————————— icpno NOT NULL VARCHAR2 (6) service_id not NULL VARCHAR2 (TEN) stat_min not null DATETIME MT_RECV_OK NOT NULL number ( 10) write an SQL statement that satisfies the following 3 conditions: Calculates the sum of the MT_RECV_OK (shown as total), and then sorts the result set in ascending order to Icpno and service_id two fields to group the resulting data stat_ Min must be greater than January 1, 2003 0:0 and less than February 1, 2003 0:0 expected output results are as follows: Icpno service_id total ———— —————— ———————————— 901001 7700 271965 9010 01 7800 3857795 Answer: Select Icpno,service_id,sum (MT_RECV_OK) total from Ismg_icp_flow where stat_min between To_date (' 2003-1 -1 ', ' yyyy-mm-dd ') and to_date (' 2003-2-1 ', ' Yyyy-mm-dd ') group by icpno,service_id order by total;
34 Database Common face questions explained