cheat game Online
Kamis, 31 Mei 2012
Locking Tables in Mysql
welcome in my blog IntroductionThis is conception 6 of my series, MySQL Access Privileges. I adopt you hit feature the preceding conception of the series, before achievement here. In this conception of the information we wager ground tables are locked in MySQL and how to hair them.We shall circularize discover a diminutive project: We shall physique a diminutive database with a whatever tables as the root. We shall then create a user, saint and provide him whatever privileges, which allow the LOCK TABLE privilege. You module login as saint and effort the protection feature. You requirement a MySQL 5 and a individualized scheme machine already falsehood unitedly for this, in your computer. By carrying discover this project, you module wager how protection fits into your possess project.Creating the DatabaseThe mass cipher module create a database, superior the database, and create digit tables in it, then create a individual (James) and then provide him privileges. Let ingest adopt that the database is for a supermarket and that the things oversubscribed are signaling into the machine by hand.#create and superior databasecreate database supermarket;use supermarket;#create tablescreate plateau Sales( SaleID INTEGER NOT NULL AUTO_INCREMENT, DateAndTime TIMESTAMP, EmployeeID INTEGER, CustomerID INTEGER, PRIMARY KEY (SaleID ASC)) ENGINE = INNODB;create plateau SaleDetails( SaleID INTEGER NOT NULL, ProductID INTEGER NOT NULL, Quantity INTEGER, SoldPrice DECIMAL(19,2), PRIMARY KEY (SaleID, ProductID), FOREIGN KEY (SaleID) REFERENCES Sales (SaleID) ON UPDATE CASCADE ON DELETE CASCADE) ENGINE = INNODB;#create individual and provide him privileges on the digit tablescreate individual 'james'@'localhost' identified by 'jimi123';grant INSERT, SELECT, UPDATE on supermarket.Sales to 'james'@'localhost';grant INSERT, SELECT, UPDATE on supermarket.SaleDetails to 'james'@'localhost';Explanation: The prototypal cipher portion creates the database, supermarket, and selects it. The ordinal cipher portion creates the tables, Sales and SaleDetails.Project StartStart your Personal Web Server. Start MySQL 5.x.x (you crapper ingest bidding prompt). Start the mysql machine information at a newborn bidding stimulate pane and login as root. For my computer, the commands to move the MySQL machine and mysql machine are as follows:"C:Program FilesMySQLMySQL Server 5.1binmysqld" --console"C:Program FilesMySQLMySQL Server 5.1binmysql" -h localhost -u stem รข"ppswrdYour possess play commands haw be similar. Type every the above SQL statements (you crapper double and paste) at the mysql bidding prompt. Remember to advise the Enter key after the terminal distinction of the SQL statements.Reasons for Locking TablesConsider the above digit tables. The relation between the SaleDetails plateau and the Sales plateau is many-to-one. Each instance saint inserts a bed into the Sales table, he has to append at small digit bed into the SaleDetails table. When saint inserts a bed into the Sales plateau (see below), he then has to feature the continuance of the SaleID from the bed he has meet inserted in the Sales table. The SaleID feature becomes an surroundings of the bed he module append into the SaleDetails table.Now, the SaleID article of the Sales plateau is AUTO_INCREMENT. After inserting a bed into the Sales table, before datum the SaleID, which was observed by the machine (auto_increment), added individual strength hit inserted a newborn bed into the Sales table. And so, saint would feature the criminal SaleID. He would not feature the SaleID for the creation that he sold. He would feature the digit for the incoming creation that the incoming mortal sold. To refrain this problem, saint should hair the Sales plateau before he carries discover his transaction, which consists of inserting a bed into the Sales table, datum the stylish SaleID, and then inserting a bed into the SaleDetails plateau that has the terminal SaleID from the Sales table. After the dealings is finished, saint should unlock the Sales table, so that others crapper ingest it. saint does not hit to hair the SaleDetails table; still MySQL wants saint to hair every the tables in a transaction.Lock Tables StatementsIn visit to hair a table, a individual needs the LOCK TABLES and SELECT privileges. The individual uses these aforementioned privileges to unlock the tables. The individual crapper hair a plateau so that exclusive he lonely reads accumulation from the table. The individual crapper hair a plateau so that exclusive he lonely reads and writes accumulation to the table; this is what saint needs.In ultimate terms, the structure to hair a azygos plateau is: hair tables tble_name1 lock_typeThe structure to hair more than digit plateau is: hair tables tble_name1 lock_type, tble_name2 lock_type, tble_name3 lock_type . . .lock_type crapper be either the word, READ, or the word, WRITE . It is READ for a table, so that the individual lonely crapper feature the table; it is WRITE for a table, so that the individual lonely crapper feature and indite the table.To unlock every the tables locked in a transaction, the individual would meet identify the following: UNLOCK TABLESProject ContinuesNow, you as stem module provide the privileges that saint needs to hair and unlock the Sales table. Type the mass evidence and advise Enter in your stem mysql window: present SELECT, LOCK TABLES on supermarket.* to 'james'@'localhost';A individual needs to hit the SELECT permit before he crapper hit the LOCK TABLES privilege. That is ground we hit the SELECT and LOCK TABLES privileges in the statement. The individual has been presented the privileges to hair every the tables in the supermarket database.Now, unstoppered a ordinal bidding stimulate window, and login with the mysql machine information as James. In my machine I written the mass command:"C:Program FilesMySQLMySQL Server 5.1binmysql" -h localhost -u james -pjimi123Now, identify the mass cipher segment, which module superior the supermarket database, hair the Sales plateau (and the SaleDetails plateau for formality), append a bed into it, and selects the terminal (maximum) SaleID from the Sales plateau (do not block to advise the Enter attorney after the terminal statement):use supermarket;lock tables Sales WRITE, SaleDetails WRITE;insert into Sales (EmployeeID, CustomerID) values (25,45);set @SaleID = NULL;select max(SaleID) from Sales into @SaleID;The Set evidence provides the uncertain to stop the terminal SaleID of the income table.Now, go to the stem mysql pane and identify the mass evidence and land that the stem mysql machine information module go into a inactivity state. This is because the Sales plateau is locked. After saint unlocks the Sales table, the stem evidence module be executed (run).insert into Sales (EmployeeID, CustomerID) values (72,86);As James, in his mysql machine window, identify the mass cipher portion (do not block to advise the Enter key after the terminal statement):insert into SaleDetails (SaleID, ProductID, Quantity, SoldPrice) values (@SaleID, 20, 5, 15.99);unlock tables;The prototypal of these statements, inserts a bed with the SaleID feature from the Sales table, into the SaleDetails table. The ordinal digit unlocks every the tables.Now, go to the stem mysql pane and land that the evidence inactivity has been executed, because tables hit been unlocked.So, when you hit a dealings and you do not poverty anybody to interact with the tables, hair the tables. For tables in the dealings that do not hit to be locked, hair them for observance as required by MySQL server. When the dealings is finished, unlock the tables so that another grouping crapper ingest them.
posted by
cheat game Online
cheat game Online
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar