Operating table SQLServer learning
Relational database typically contains more than one table. Actually a collection of database tables, the database data or information is stored in the table. Data table is stored in a logical structure and operation of each table represents an object of significance to the user.
For example, in front of an additional database, to contain emp
, dept
other forms
type of data
Before you create a table, or that before we use the database, you must first understand about data type definitions, as we learned when programming, you must first learn how to define variables the same.
defined sqlserver a lot of data types are available. For details please check the relevant information, just to name some of the common data types, about a dozen, divided into numeric, character, date, type and binary type
Value Type Data Source:
int
: Plasticfloat
: Floatdecimal
: Precise type, note that there is no double type of SQLserver
Character data types:
char
: Fixed-length data --- char (10), save the data length of 10 characters, regardless of whether the 10 characters, you need to allocate space for 10 characters, but there is a benefit of the type of data query efficiency is very highvarchar
: Variable length character data ----> length may vary with the length of stored content is changed, you can save disk spacetext
: Large text data type, storage of large text data
Date of types of data:
datetime
: Represents the date and time, this data type to store all of January 1, 1753 to December 31, 9999 the day from the date and time data, accurate to three hundredths of a second, or 3.33 millisecondsSmalldatetime
: Indicates the date and time from January 1, 1900 2079 June 6 during the day, accurate to a minute
Binary data types:
binary
: Storing binary data about the length of 8000 bytes, the data type is a fixed length
varbinary
: Storing about 8,000 bytes in length binary data, the data type of a variable length
image
: Image data type used to store binary data of variable length, up to about 231-1 or 2,000,000,000 bytes
Create a table
To be able to save the data we need in the database, a relational database is used to store a memory storage of a table structure.
Create a table in the database there are two ways:
- In the tool
Microsoft SQL Server Management Studio
to create a way of using visual (not recommended) - Create a way to write SQL commands
Because they do not recommend creating tables directly using direct visualization of the way, no longer talk here
Also in writing SQL commands Microsoft SQL Server Management Studio
in
case study:
Creating a student table
create table student ( id int, Name varchar(50), clazz varchar(20), birthday date, Living_expenses float, achievement float, discription text );
As above, the implementation of the above command, you can create one of the most simple form student
, and
create table
I need to create a form on behalf ofstudent
: Table Name- In brackets is the column of the table, you Form I need to create the column names and data types that can be saved
id int,
Data type is represented by the field name called id column, can be saved isint
the type of this and our normal programming practice is reversed, please note.- Field, not a complete definition of the field, separated by commas are needed, but in the last field can not have a comma,
- Create a complete statement, please end with a semicolon.
Modify table
Sometimes in the table created, but later found not to create the perfect table, need to be modified
Modify the table table and column names: The
student
change table namestudents
- Want to modify the table names and field names, you need to use sql stored procedure is provided (in relation to the stored procedure update later):
sp_name
exec sp_rename 'student', 'students';
- Want to modify the table names and field names, you need to use sql stored procedure is provided (in relation to the stored procedure update later):
Modify the column name: the
students
tableid
to modify columns tostudentid
- Modify the column name is used in the same
sp_name
stored procedure to modify, to note that, in the modification of a column name, the table name can be modified need to bring in the original column names, otherwise an error.
exec sp_rename 'students.id', 'studentid', 'column';
- Modify the column name is used in the same
Modified column data type: the
name
type of data to be modifiedchar(50)
;- Modifying the data type of the column, without any memory through the direct use
alter table
keyword to
alter table students alter column name char(50);
- Modifying the data type of the column, without any memory through the direct use
Add a field: Add a table in the students
password
field is used to save passwords learning- In time of need, we need a Sword field for storing new content
alter table students add password nvarchar(20);
Delete table
In general, there are two ways to delete the table
One is to delete the data in the table, leave the table structure, can be used when needed to restore the log
delete from students;
One is the direct delete data and table structure can not be restored.
drop table students;