This note is mainly about creating tables, creating constraints, adding and deleting databases.
Experience: 1, it's best to consider what data to use when building a table, because using inappropriate data wastes space 2, each table should have a link, no Contact table is meaningless .3, make the table and do the constraints separate to do ... Look clear and easy to understand4, the field is best to buckle up with []===========================================
Knowledge Points:
One, there are several types of data in SQL 1, exact number: tinyint (0~255), smallint ( -32768~32767), int (2^31 size), bigint 2, approximate number: real (10^-38) float (10^-308) is the number of digits after the decimal point is different, remember that SQL does not have a double this type 3, date, Time: datatime (1953.11~9999.12.31, accurate to 0.003,0.007,0.000 (e.g. 0.002 rounds to 0.003)) datatime2 (0000.1.1~9999.12.31) (accurate to 100 nanoseconds) Smalldatatime (1990.1.1~2079.6.6 accurate to minutes) 4, string: char (fixed length, 1~8000) varchar (variable length 1~8000) if greater than 8,000 with text 5,Unicode string (Chinese string): Nchar,nvarchar A kanji account for two bytes, so nchar (10) takes up 20 bytes, and char (10) accounts for 10 bytesin the internationalized development software to use Unicode string, otherwise cannot display Chinese ...=================================================template for building a tableCREATE TABLE Table name (
Column name type [constraint],---plus [] is can have no
....
....
)============================================constraints for SQL:one, Entity integrity constraints1, PRIMARY KEY constraint (PK): Features: Do not repeat, not empty, each table must have a primary key--primary key (...)2, Unique constraint (UK): Feature: No repetition, a table can have multiple unique constraints, but only one is empty (two is not unique. because empty = = empty). Unique3, identity column: auto-grow, seed N, Growth X, can only be used in integral type---identity (100,1) initial value 100, one-time self-increment 1 two, domain integrity1, non-null NOT NULL2, DEFAULT constraint (DF): Defaults value for (column name)3, check Constraint (CK): check (condition)4, foreign Key (FK): foreign key created in from table, connect to primary key in primary table----foreign key. References .... third, referential integrity constraints1, foreign key: not yet spoken Four, custom constraints (next semester Learning---> Triggers) Note: The primary table cannot delete the foreign KEY constraint from the table, only cascade delete can, but the cascade Delete has a disadvantage, that is, the wrong data can not be deleted recovery. =====================================--------------template to create constraints---------------ALTER TABLE table nameadd constraint constraint name (first name begins with the letter of the constraint eg:pk_id, fk_id)the specific ConstraintsGo
eg
-- -Self-increment Create Table user_info (user_idintprimarykeyidentity(1, 1), //user_namevarchar( not NULL , ....)
-------------template that establishes a PRIMARY KEY constraint------------------
Alter Table Stuinfo Add constraint Pk_sid Primary Key ([SID]) Go
---------------Establish a DEFAULT constraint-------------------
Alter Table addconstraint df_sexdefault1for[ Sex]go
--------------------Establish a check constraint--------------
Alter Table Stumark Add constraint Ck_score -- -------------------------------This is not a SQL notation, so use another------check (score>=0 and score<=100) Check between 0 and +) -- -it must be from small to large. Better use this. Go
----------------FOREIGN KEY constraint-----------------------
Alter Table Stumark Add constraint fk_id Foreign Key [ID] References [ID] )go
------------Unique Constraint-----------------
Alter Table Stuinfo Add constraint CK_SFZ Unique [SFZ] ) --SFZ ID Field
========================================DML: Increased--------------1, single insert----------------insert INTO table nameVALUES (...) All values in the table (except for autogrow) eg:
Insert into Stuinfo Values ('YL',default) Write default placeholder even if it is
---------------2, multiple insertion-------------------
Insert into Table name Select value 1,..... Value n UnionSelect value 1,..... Value n Union.... Select value 1,..... Value ngo
//Note that multiple inserts cannot be used with default values ... is not to write default eg:
Insert into Stuinfo Select ' YL ',1UnionSelect'ZX',0Go
-------------new data from existing tables in Shanghai--------------SELECT * into new table name[where filter] //This code will go wrong if it runs two times because it will show that the new table already exists ================ Delete ==============Delete from table name----- Just delete the data, do not delete the table, delete the table with drop [where filter] ============= modifying =============Update table nameset field 1= ..., field 2= .....[where filter] eg:
Update Stumark Set Score+ =5update stumarkwherein ('zs ','yz','aaa'....)
of course it can be not in (except for these)