• 数据表的基本操作(含代码)



    /**创建表并修改结构**/
    /*
    功能 : 创建一新的二维表  
    语法:
    create table 新表名
    (字段名1 字段类型 { [null | not null] [primary key| unique] },
    字段名2 字段类型 { [null | not null] [primary key| unique] },
    字段名3 字段类型 { [null | not null] [primary key| unique] },
    ……
    )
    提示:{…}为可选项,[…|…] 表示二者只选其一
    */
    /*
    --1.新建数据库rj2001,并作为当前数据库,在rj2001中创建客户信息表(customer),包含下列字段:
    客户编号cid char(4) 非空
    客户姓名cname varchar(20) 非空
    联系电话tel char(11)非空
    地址address varchar(50)
    邮箱email varchar(40)
    */

    create database rj2001
    use rj2001
    go
    create table customer(
    cid char(4) not null,
    cname varchar(20) not null,
    tel char(11) not null,
    address varchar(50),
    email varchar(40)
    )

    --2.查询客户信息表customer
    select cid,cname,tel,address,email from customer


    /*
    修改表结构:

    功能 : 更改、添加、删除表的定义  
    语法:
    更改字段:alter table 表名 alter column 字段名 新字段类型 [null | not null]
    添加字段:alter table 表名 add 新字段名 字段类型 [null | not null]
    删除字段:alter table 表名 drop column 字段名
    ……
    提示:{…}为可选项,[…|…] 表示二者只选其一
    */

    --3.在customer表中,修改字段email长度为50
    --菜单方式:选中表名-右击-设计-修改并保存。注意:工具-选项-设计器(designers)-去除:阻止保存要求重新...方可保存
    --命令方式:
    alter table customer alter column email varchar(50)

    --4.在customer表中,增加一个字段QQ,类型varchar,长度10
    alter table customer add QQ varchar(10) null

    --5.删除customer表中字段QQ
    alter table customer drop column QQ

    --6.修改customer表中字段email,字段名改为:E-mail
    sp_rename 'customer.email','E-mail'

    --7.修改表名称:customer表名改为:customer_tb
    sp_rename customer,customer_tb

    本文章归作者所有侵权必究 若作者侵权联系删除。
  • 相关阅读:
    【SRE】华来科技SRE揭秘
    【Go】解决VS Code安装Go插件失败问题
    浏览器兼容:改写window.showModalDialog
    .Net Debugging 调试工具集
    远程调用Excel、Word、PowerPoint,服务器端设置
    使用PowerDbg自动化Windbg调试过程(转)
    调试器扩展SOSEX
    在windows server2008 IIS7.5 中用 vs2005 调试 Web 项目的注意事项
    为Visual Studio 2010添加HTML5的项目模板
    ILDasm工具
  • 原文地址:https://www.cnblogs.com/woniu11/p/13780349.html
Copyright © 2020-2023  润新知