Advanced query (1), advanced query (

Source: Internet
Author: User

Advanced query (1), advanced query (

Data Type
Integer int
Decimal double
High Precision --- Decimal)
Corresponding to the type BigDecimal in Java

String: nvarchar (32) varchar char
Char: fixed-length string (once the length is set, the space of the specified length will be occupied regardless of the actual content)
Varchar: Variable string
Nvarchar () prevents garbled characters

How to explain n in varchar (n) and nvarchar (n:
Nvarchar (n) can store up to n characters, which are not distinguished between Chinese and English.
Varchar (n) can store up to n Bytes. One Chinese character is two bytes.
Space occupied:
A single nvarchar (n) character occupies two bytes.
Varchar (n) occupies two bytes of Chinese, and English occupies one.
Value range of n:
Nvarchar (n )?? The range of n is: 1 to 4000.
Varchar (n )?? The range of n is: 1 to 8000.
Whether the size of n affects performance:
The length n in varchar and nvarchar does not affect the space size and performance. Unless n is max and the content is greater than 4000 or 8000
Setting n is more of a business requirement. For example, if the ID card is limited to 18 characters, an error is reported if there are more than 18 characters, or the hard disk is prevented from being cracked by malicious attacks. It has no effect on space and performance.
N setting is better:
Since there is no impact on space and performance, we only need to consider the business needs. I have analyzed Microsoft databases, most of which are set to 256. We will also see 64,128,512, max, etc, it may be easy to remember.

Examples of varchar (n) and nvarchar (n) buckets:
Unicode data with a variable length of n characters. The storage size of bytes is twice the number of input characters .?
The two fields have the following field values: Me and coffee?
The varchar field occupies 2 × 2 + 6 = 10 bytes of storage space, while the nvarchar field occupies 8 × 2 = 16 bytes of storage space .?
If the field value is only English, you can select varchar. If the field value contains many double-byte (Chinese, Korean, etc.) characters, nvarchar is used.
How do I select varchar and nvarchar?
In SQL Server, varchar stores data in a single byte. nvarchar uses Unicode to store data. when a Chinese character is stored in SQL Server, it is saved as two bytes (Unicode encoding is generally used) and an English character is saved to the database. If the field type is varchar, only one byte is occupied, if the field type is nvarchar, it occupies two bytes.
3. date type
DateTime

 

Parentheses must be added to the function !!!!

 

 

2. Create a database
Create database Name
Create a table
Create table Name (
Column name type (int) primary key (primary key) Auto-increment (auto_increment) attribute (not null ),
);
Add record
Insert into Table Name (column name) values (value );
Constraint addition:
1. Primary Key constraints
Alter table name 1
Add constraint pk _ column name 1 primary key table name 2 (column name 2 );
Delete primary key
Alter table Name drop primary key

2. Foreign key constraints
Alter table 'table name 1' add constraint fk _ table name 1 _ table name 2
Foreign key ('column name in Table 1') references 'table 2' (column name in table 2 );

3. unique constraints
Alter table 1 table add constraint uk_l.1 column unique key (column 1.1 );

4. Add default Constraints
Alter table name
Alter column name set default 'default ';

5. Check constraints (does not work)
Alter table name
Add constraint ck _ column name check (condition );

 

3. Foreign keys are not set.
Resolution: 1. Find that the foreign key table 1 is the primary table
2. Illegal data exists in the foreign key table
3. The types of public columns in the primary and foreign key tables must be the same

4. What is an engine?
Resolution: a program that can be started and executed by a program, also known as a driver.

5.
1. Modify the table name
Change student to student2
Alter table old table name rename new table name
2. Add Columns
Alter table 1 add 1.1 varchar (32) not null;
3. Modify the column name
Alter table name change column name new column name (type) bit not null;
4. delete a field
Alter table Name drop field name

 

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.