zoukankan      html  css  js  c++  java
  • 数据库知识总结(表结构操作)

    1.创建表Scores

    1 create table Scores               --表名
    2 (Id int identity(1,1) primary key,--设置主键,并且行号自增,
    identity(1,1)表示Id列从1开始自增,每次增加1
    3 Date datetime not null, --设置类型为datetime,不能为空 4 Name nvarchar(50) not null, 5 Score nvarchar(2) --默认状态下,类型为空 6 )

    2.修改表名Scores为NewScores

     1 exec sp_rename 'Scores','NewScores' 

    3.删除表Scores

     1 drop table Scores --删除表(表的结构、属性以及索引也会被删除) 

    4.清空表数据

     1 truncate table Scores --仅仅删除表格中的数据 

    5.修改列Score为 not null

    1 alter table Scores
    2 alter column Score nvarchar(2) not null

    6.添加列

    1 alter table Scores 
    2 add new nvarchar(20) not null

    7.修改列名

    1 exec sp_rename 'Scores.Name','NewName','column'

    8.删除列

    1  alter table Scores 
    2  drop column new 

    9.修改identity列

     如果说创建表时没有设置自增列。因为自增列不能直接修改,必须降原有Id列删除,然后重新添加一列具有identity属性的Id字段。

    1  exec sp_pkeys @table_name='Scores'                               --查询主键名
    2  alter table Scores drop constraint PK__Scores__3214EC074E3D66D2  --将主键约束先删去
    3  alter table Scores drop column Id                                --将Id列删去
    4  alter table Scores add Id int identity(1,1)                      --添加自增列(此时Id列不是主键)
    5  alter table Scores add Id int identity(1,1) constraint pk primary key  --添加自增列,设置Id为主键名字为pk

     10.修改列类型

    alter table TableName alter column ColumnName varchar(50)         --将一个列的类型改成varchar(50)
    alter table student change name name varchar(20)                  --另一种修改方法
    休对故人思故国 且将新火试新茶 诗酒趁年华
  • 相关阅读:
    本地连不上远程mysql数据库(2)
    linux后台执行命令:&和nohup
    uva 10806 Dijkstra, Dijkstra. (最小费最大流)
    VS2013带来的"新特性"
    HDOJ 5091 Beam Cannon 扫描线
    2014百度之星资格赛4题
    二维码登陆
    安装Sublime配合quick-cocos2d-x开发
    J2SE核心开发实战(二)——字符串与包装类
    LeetCode_3Sum Closest
  • 原文地址:https://www.cnblogs.com/sdadx/p/5992685.html
Copyright © 2011-2022 走看看