Common SQL exercises (with answers)

Common SQL problem

First, the concept of a common database

1. The role of the trigger?

 

A trigger is a special stored procedures, mainly triggered by events to be executed. It can strengthen the constraints, to maintain the integrity and consistency of the data, you can track actions within the database in order to not allow unauthorized updates and changes. You can cascade operation. For example, the table contains data on the operation of the other trigger on a table, which in turn causes the operating table the trigger is triggered.

 

2. What is a stored procedure? To call what?

 

A stored procedure is a precompiled SQL statement, the advantage of allowing a modular design that is created once, later in the program can call multiple times. If you need to do many times a particular SQL, stored procedures using simple SQL statement is executed faster than that. You can use a command object to call a stored procedure.

3. The role of the index? What are its advantages and disadvantages?

Index on a special table, a database search engine can use it to speed up retrieval of data. It is very similar to the real-life book catalog, you do not need to query the contents of an entire book to find the data you want. Index may be unique, allowing to create an index to specify a single column or a plurality of columns. The disadvantage is that it slows the speed of data entry, but also increases the size of the database.

4. What is a memory leak?

Generally we are talking about a memory leak refers to the heap memory leak. Heap memory is allocated from the heap for the program, of any size, after use to display free memory. When an application using the new keyword to create an object, etc., allocated from the heap it on a block of memory, use the program after calling free or delete release the memory, the memory can not be said otherwise use, we say that the memory is leaked.

5. Maintain the integrity and consistency of the database, since you like it or write the business logic with flip-flops? why?

I do so, as far as possible the use of restraint, such as check, primary key, foreign key, non-empty fields, etc. bound to do so the most efficient and most convenient. Second is the use of triggers, this method can be guaranteed, no matter what business systems can access the database to ensure the integrity and consistency of the new data. A final consideration is self-written business logic, but the trouble to do so, the programming complexity and inefficiency.

6. What is a transaction? What is the lock?

Transaction is to be bound together as a logical unit of work packets SQL statement, a statement, if any operation fails then the whole operation was a failure, after the operation will be rolled back to the state before the operation, or has the nodes. In order to ensure either executed or not executed, you can use the transaction. To have a group of statements considered as a transaction, you need ACID  test , namely atomicity, consistency, isolation and durability.

Lock: So in the DBMS, the lock is the key to the transaction, the lock can ensure the integrity and transaction concurrency. As in real life in the lock, which allows owners of certain data, some data can not be used within a certain time or data structure . Of course, the lock also points level.

7. What is a view? What cursors are?

A view is a virtual table, has the same functionality as a physical table. Can be increased to view, change, operation, there is usually attempted to a table or a subset of the plurality of rows or columns of the table. Changes to the view do not affect the basic table. It allows us to obtain data more easily, compared to multi-table queries.

  Cursor: as a means to effectively process the query result set out. The cursor can be set in a particular cell line, the current line retrieve one or more rows from the result set. You can make changes to the current row result set. Generally do not use a cursor, but need to process the data one by one when the cursor is very important.

8. What is the primary key? What is a foreign key?

A primary key is the form of the (one or more) fields, only to define the form of the line; in the primary key value is always unique. A foreign key is a constraint to establish the relationship between the two tables. This relationship typically involves a table where the primary key field with another table (although it may be the same table) in a range of fields linked. Then the foreign key field is connected.

9. four features of database transaction?

A well-designed database can help us to ensure that the transaction has four properties (ACID):

Atomic : Atomic refers to the transaction is an indivisible unit of work, either all operations in a transaction happen or none happen.

Consistency : If you perform a database before the transaction is a complete state, then after the end of the transaction, whether the transaction is successful, the database remains a complete state.

  The complete state of the database: a database when all the data are in line with all the constraints defined in the database, the database can now be called a complete state.

Isolated : When multiple users concurrent access to the database, a user's transaction can not be firm interfere with other users, data between multiple concurrent transactions to be isolated from each other.

Persistence : that once a transaction is committed, his impact on the database is permanent.

If two threads a modification, a query:

 Dirty read: A transaction reads data to another uncommitted transactions.

 Magic Reading: in the current transaction, a read transaction data submitted by the insertion of another.

 Non-repeatable read: in current affairs, read to the data submitted by another transaction to update or delete.

10. Three Forms database?

 The first paradigm (1NF): For adding a requirement specification, all the fields should be atomic, i.e., each column of the database table are indivisible atomic data item, is not set, arrays, records nonatomic data item. Entity i.e. a property has multiple values, it must be split into different properties.

 Second Normal Form (2NF): established on the basis of first normal form (1NF) on that second normal form (2NF) must be met first normal form (1NF). The second paradigm (2NF) requires each instance of a database record or table must be uniquely distinguished. Required attributes of the entity totally dependent on the primary key. The so-called completely dependent refers not only depend on the presence of the main part of the key attributes

 The third paradigm (3NF): on the basis of the 2NF, any non-primary property not dependent on other non-primary property (transfer to eliminate dependency on the basis of the 2NF)

 

 

Second, write simple SQL:

1. Use a SQL statement to query the records were greater than 80 names of the students?

// 第一种方式:
select name from table where name not in ( select name from table where score
< 80 );

// 第二种方式:
select name from table group by name having min(score) >= 80;

 

2. In addition to automatically delete different id number, other students have the same redundant information?  

Fields are: id, stunum, name, course, score

delete table where id not in ( select min(id) from table group by stunum,name,course,score );

3. a team called table, there is only a field name, a total of four records, respectively, a, b, c, d, correspond to the four ball, the ball is now four of the game, all the display with a sql statement possible combinations of the game?

// all combinations: ab, ac, ad, bc , bd, cd letters are all smaller than the left, the right letter 
select a.name, b.name from team a, team b where a.name <b.name order by a .name, b.name;

4. Amount check out all of the months from TestDB data sheet than 101 subjects with high occurrence amount corresponding month of subjects?

AccID: account code, Occmonth: Amount month, DebitOccur: Amount

select a.AccID from TestDB a,( select Occmonth,max(DebitOccur) from TestDB where AccID = '101' group by Occmonth )b 
 where a.Occmonth = b.Occmonth and a.DebitOccur > b.DebitOccur;

5. Such a table how the child
year month The AMOUNT
1991. 1 1.1
1991 2 1.2
1991 1.3. 3
1991. 4 1.4
1992 2.1. 1
1992 2 2.2
1992 2.3. 3
1992 2.4. 4
investigation as a result of such
year M1 M2 M3 M4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4 

select year, 
(select amount from table m where month=1 and m.year=aaa.year) as m1,
(select amount from table m where month=2 and m.year=aaa.year) as m2,
(select amount from table m where month=3 and m.year=aaa.year) as m3,
(select amount from table m where month=4 and m.year=aaa.year) as m4
from table group by year

6. Copy table (only replicated structure, source table names: a new table name: b) 

select * into b from a where 1 <> 1; // where1 = 1, copy the contents of the table structure data and 
the Oracle : Create Table B
      AS
      SELECT * WHERE. 1 from A = 2;

7. A copy of the table (copy the data source table name: a target table name: b) 

 insert into b(a, b, c) select d,e,f from a; 

8. display the article, the author and the final response time

select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b;

9. Schedule five minutes in advance to remind

select * from schedule where datediff ( 'minute', f start time, getdate ())> 5;

10. The two related tables, delete the main table has no information on the side tables

delete from info where not exists (select * from infobz where info.infid=infobz.infid );

11. There are two tables A and B, both key and value two fields, if key B in A have, B value is changed to put the corresponding value A?

update b set b.value=(select a.value from a where a.key=b.key) where b.id in(select b.id from b,a where b.key=a.key);

 

12. The four tables are known as follows:

Student Table: STUDENT (S #, SNAME, SAGE, SSEX)

Curriculum: COURSE (C #, CNAME, T #)

Results Table: SC (S #, C #, SCORE)

Teacher Table: TEACHER (T #, TNAME)

 

(1) query program number "001" course than the "002" of the course grade all students learn numbers?

select x.sno,x.score,y.score from sc x,sc y
where  x.cno=1001 
   and y.cno=1002
   and x.sno=y.sno
   and x.score > y.score;

(2) query average score greater than 60 points the student's school number and grade point average?

select sno,avg(score) from sc
group by sno
having avg(score)>60;

(3) query for all students number, name, number of elective total score?

select sc.sno,sname,count(cno),sum(score)
from student join sc on student.sno=sc.sno
group by sc.sno,sname;

(4) the query name to "see" the number of teachers?

select count(Tname) from teacher
where Tname like '悟%';

(5) query never learned "Wukong" teacher class student school, name?

select sno,sname from student 
where sno not in(select sno from SC where cno in(select cno from course
where tno in(select tno from teacher
where tname='悟空')));

 

Guess you like

Origin www.cnblogs.com/kanglijun/p/11040030.html