SQL_ Experiment 2.1.3 Tsinghua University Press

Source: Internet
Author: User

This article originates from: Http://blog.csdn.net/svitter

Experimental objectives: Familiar with entity integrity, referential integrity, transaction processing;

/*1. Creating table Stu_uion in database School table, PRIMARY KEY constraint, inserting and updating a record without violating entity Integrity */use School CREATE TABLE stu_uion (Sno char (5) NOT NULL Unique,sname Char (8), Ssex char (1), Sage Int,sdept char (a), constraint Pk_stu_uion primary key (SNO)); Insert Stu_uion VALUES (' 10000 ', ' wangmin ', ' 1 ', and ' CS '); update stu_uion Set sno = ' WHERE sdept = ' cs '; update stu_uion set sno = ' 9200 2 ' Where sname = ' wangmin '; select * from stu_uion;/*2.3. Demonstrates inserts that violate entity integrity, update operations */use Schoolinsert stu_uion values (' 10000 ', ' Li h UA ', ' 1 ',/*unique*/update, ' CS '); stu_uion set sno = NULL where sno = ' 10000 '; /*not Null*//*4. Demonstrates the processing of a transaction, including the establishment, processing, and fallback of an error transaction */use schoolset xact_abort on/* setting Xact_abort to ON, If an error occurs in the transaction (transaction) statement, the entire transaction will be rolled back * If set to OFF, only the error rollback statement */begin transaction t1insert into stu_uion values (' 95009 ', ' Li  Yong ', ' M ', +, ' ee '), insert into stu_uion values (' 95003 ', ' Wang Hao ', ' 0 ', ' "EE '), insert into stu_uion values (' 95005 ', ' Wang Hao ', ' 0 ', ' EE '); select * from Stu_uion; commit transaction t1/*5. Insert data by creating a scholarship table to demonstrate current and existing data environmentEntity integrity and referential integrity cannot be established when unequal */use schoolcreate table Scholarship (m_id varchar), stu_id char (TEN), R_money int); INSERT INTO Scholarship VALUES (' 0001 ', ' 700000 ', ', '), insert into scholarship values (' 0001 ', ' 800000 ', ', "); select * FROM Scholarsh Ip;/*constraint*/use schoolalter Table Scholarship addconstraint pk_scholarship primary key (m_id);/*pk:primary key*//* There are two 0001, the data in the PRIMARY KEY constraint *//**scholarship cannot be established, the SID correspondence in the STU_ID and students tables is not met, and referential integrity failures are created */use schoolalter table Scholarship AddConstraint fk_scholarship foreign KEY (stu_id) references students (SID);


 

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.