- we have to faculties, classes and students to provide examples. TABLE the Create [dbo] .YuanXi ( Id int IDENTITY ( 1 , 1 ) the NOT NULL, - school id from incremental YuanXiName VARCHAR ( 50 ) null , - faculty name ) the Create TABLE [dbo] .Class ( Id int IDENTITY ( . 1 , . 1 ) the NOT nULL, - class id from incremental YuanXiID int null , - Department id ClassName VARCHAR ( 50 ) null - class name ) Create TABLE [the dbo] .Student ( idint IDENTITY ( 1 , 1 ) the NOT NULL, - student id from incremental ClassID int null , - class id StudentName VARCHAR ( 50 ) null , - student's name ) - This table is a picture of the table, I temporarily added, this may be followed by a demonstration of the Create TABLE [dbo] .PathImg ( id int IDENTITY ( 1 , 1 ) the NOT nULL, - school increment id from TableName VARCHAR ( 50 ) null , - this is to show, for example, I want information Engineering academic pricing picture, it would have to table Hinako XuanXi table plus the change in the table, as well as id School of information Engineering, plus TableNameId int null- This is a one of a list of the above mentioned id Path VARCHAR ( 50 ) null - storage class photo path )
The change is relatively easier, because when modifying the primary key will not change (that is, the table increment the id), as additional information would change does not affect the other table, even if the impact, for example, I modified the YuanXi table School of information Engineering School of Art and design changed
Although the name changed, but Id has not changed, and we may say that the corresponding Class table Class not have to change anything, yes, that needs to change, but we can click on the Edit button in the interface for editing the class list re-naming of the class i to the line.
Id has not changed since. If you do not want to edit, then find a class list of those classes to delete, delete the line or batch, and delete the way in my notes ( https://www.cnblogs.com/zpy1993-09/p/11786057.html ) It has been done in detail.
Here I want to modify another way, the above table does not seem applicable scenario, and the above table does not matter. Now I want to say is my favorite in the development of a modification of the way, with a certain logic to modify when this type of modification, not modify a table so dead simple . Or give you an example! This example is a web platform and mobile APP with each other.
For example, a task list, a task step table and a task template list, (in fact, need to step template table, added task belongs to which task templates, step added belongs step template, so that it is reasonable, but here is simplified, the direct use of three tables)
Task list I add a task in the table, and this task needs to be done in three steps can be considered to complete the task. I assigned this task to the Joe Smith, Joe Smith and open the phone APP should see the task of their own. He has to do is put the three steps to complete and submit the data and stored in the steps in the table. The table was built up in order to see to understand:
TaskStateID: Id represents a task state numbers represent: 1: Not started 2: Processing 3: Pending 4: Completed
TaseMoBanID: indicates which tasks belong to this task template
Specific process: the tasks assigned to Joe Smith, TaskStateID = 1 task does not start, if Joe Smith completed part of the task, TaskStateID = 2 mission has been in process when Joe Smith put all of the steps are done, TaskStateID = 3 task belongs pending. Finally, there is the task of auditing the initiator of the audit, the review is that the task has been completed TaskStateID = 4
For example, my task has been assigned to Joe Smith, then TaskStateID should be 1, the task has not yet started,
We know the task you want to complete three steps, Joe Smith each completed a task, he will submit once. To add a record in the step table, do not add here that the said modification, this time we should put Task table TaskStateID status changed to 2 processing. But when modifying the database have no idea, you in the end to complete a number of steps, he would revise, you can only be done one step, it could have done. This requires us in using logic to determine the database.
First, determine the current Joe Smith completed a number of steps, when the first submission will spread to the database two parameters; one is: TableName: name of the table TableNameID: Task ID
We are here to facilitate later write directly define stored procedures:
@TableName varchar(30)
@TabaleNameID int
1. Find the number of records from the table in step
select Count(*) from Step where TableName=@TableName and TableNameID=@TableNameID
2, the number of steps need to find to complete the task altogether. (Here we must note that this modification is generic, so TableName pass over the name of a table is a table in the database. Database is to check the string table by name, and so not the same as the above, the need to string concatenation into the sql statement)
First find the id of the task template
EXEC ( 'SELECT TaskMoBanID FROM' + @ TableName + 'where Id =' + @ TableNameID) - translated phrase sql code that it is the implementation of sql statement: select TaskMoBanID from Task where Id = @ TableNameID
Through the task template id to find the number of steps needed to complete the task:
select MoBanStepCount from taskMoBan where Id ID = step to give the
3, Joe Smith currently committed to take several steps to complete a number of steps to complete the task with comparative figures (note that the number of steps in the open seating on when APP has three steps show, he just need to edit is complete, he does not ability to add steps, step template has been limited to dead)
If the current step number of the step <accomplish this task number
The modified task status TaskStateID = 2 (Processing)
update Task set TaskMoBanID=2 where Id=@TableNameID
else
update Task set TaskMoBanID=3 where Id=@TableNameID
Tidy write a stored procedure
The USE [ the TestData ] the GO the SET the ANSI_NULLS the ON the GO the SET the QUOTED_IDENTIFIER the ON the GO the ALTER PROCEDURE [ the dbo ] . [ AlterDemo ] @tablename VARCHAR ( 30 ) - the string table @TabaleNameID int - table ID the AS - define two variables to receive the current number of steps to complete the task and the total number of steps DECLARE @CurrentStepCount int DECLARE @SumStepCount int - query the current number of records, and assigned to the variable the SET @CurrentStepCount= ( The SELECT COUNT ( * ) from the Step the WHERE TableName = @tablename and TableNameID = @TableNameID ) - Total queries steps to complete the task number, and assigned to the variable - the scarlet letter, the definition of this variable must be placed inside a character if, like the first two, like on the outside, the error will execute this statement, we must pay attention to the SET @SumStepCount = EXEC ( ' delcare @TaskMoBanID VARCHAR (30) the SELECT @ TaskMoBanID TaskMoBanID the FROM = ' + @tablename + ' the WHERE Id = ' + @TableNameID + 'select MoBanStepCount from taskMoBan where Id=@TaskMoBanID') if @CurrentStepCount<@SumStepCount update Task set TaskMoBanID=2 where Id=@TableNameID else update Task set TaskMoBanID=3 where Id=@TableNameID
- above EXEC () statement is executed character translation general sql statement:
delcare @TaskMoBanID VARCHAR (30) // task templates id temporary storage variable
SELECT @ TaskMoBanID = TaskMoBanID FROM Task where Id = @ TableNameID // accepts queries with the variable results
select MoBanStepCount from taskMoBan where Id = @ TaskMoBanID // according to the results of the query, the query template table again in the steps of the task template number
Well, modify stop here, in fact, are generally single table, the primary key after all modification time without changes, and other influences on the platform directly in the editor on the line. Either delete the line, but some easier way, but as I said above that with a logic model, it would have to honestly write sql code.