Operating table SQLServer learning

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, deptother 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 : Plastic
  • float: Float
  • decimal: 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 high
  • varchar: Variable length character data ----> length may vary with the length of stored content is changed, you can save disk space
  • text: 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 milliseconds
  • Smalldatetime: 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 Studioto 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 Studioin

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 tableI need to create a form on behalf of
  • student: 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 is intthe 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 studentchange 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';
  • Modify the column name: the studentstable idto modify columns tostudentid

    • Modify the column name is used in the same sp_namestored 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';
  • Modified column data type: the nametype of data to be modified char(50);

    • Modifying the data type of the column, without any memory through the direct use alter tablekeyword to
    alter table students alter column name char(50);
  • Add a field: Add a table in the students passwordfield 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;

Guess you like

Origin www.cnblogs.com/fenglinyu/p/11069153.html