SQL ALTER TABLE

本文介绍如何使用 SQL 的 ALTERTABLE 语句来添加、删除或更改现有表中的列。通过具体实例展示了如何添加新列、改变现有列的数据类型以及删除不再需要的列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ALTER TABLE 语句

ALTER TABLE 语句用于在已有的表中添加、修改或删除列。

SQL ALTER TABLE 语法

如需在表中添加列,请使用下列语法:

ALTER TABLE table_nameADD column_name datatype

要删除表中的列,请使用下列语法:

ALTER TABLE table_name DROP COLUMN column_name

注释:某些数据库系统不允许这种在数据库表中删除列的方式 (DROP COLUMN column_name)。

要改变表中列的数据类型,请使用下列语法:

ALTER TABLE table_nameALTER COLUMN column_name datatype原始的表 (用在例子中的):

Persons 表:

IdLastNameFirstNameAddressCity1AdamsJohnOxford StreetLondon2BushGeorgeFifth AvenueNew York3CarterThomasChangan StreetBeijingSQL ALTER TABLE 实例

现在,我们希望在表 "Persons" 中添加一个名为 "Birthday" 的新列。

我们使用下列 SQL 语句:

ALTER TABLE PersonsADD Birthday date

请注意,新列 "Birthday" 的类型是 date,可以存放日期。数据类型规定列中可以存放的数据的类型。

新的 "Persons" 表类似这样:

IdLastNameFirstNameAddressCityBirthday1AdamsJohnOxford StreetLondon 2BushGeorgeFifth AvenueNew York 3CarterThomasChangan StreetBeijing 改变数据类型实例

现在我们希望改变 "Persons" 表中 "Birthday" 列的数据类型。

我们使用下列 SQL 语句:

ALTER TABLE PersonsALTER COLUMN Birthday year

请注意,"Birthday" 列的数据类型是 year,可以存放 2 位或 4 位格式的年份。

DROP COLUMN 实例

接下来,我们删除 "Person" 表中的 "Birthday" 列:

ALTER TABLE PersonDROP COLUMN Birthday

Persons 表会成为这样:

IdLastNameFirstNameAddressCity1AdamsJohnOxford StreetLondon2BushGeorgeFifth AvenueNew York3CarterThomasChangan Street
/ -- 在线客服聊天 --> 认证失败,重新设置聊天系统的用户密码 // 聊天窗口右侧默认展示最近订单,如果想要展示商品、订单、店铺则需要在当前页面中设置隐藏域,name必须为 chat_goods_id, // chat_order_id, chat_supp_id /* ------------------------------------------------------ */ function action_authfail () { $user_id = $_SESSION['user_id']; $sql = "select user_name, password, email from " . $GLOBALS['ecs']->table('users') . " where user_id = '$user_id'"; $row = $db->getRow($sql); $success = create_of_user($user_id, $row['password'], $row['user_name'], $row['email'], 10, - 1); if($success) { $result = array( 'error' => 1,'message' => '可能由于网络原因,发生错误!请点击 <a href="chat.php?act=chat"><strong>重试</strong></a> ,重新连接...','content' => '' ); } else { $result = array( 'error' => 1,'message' => '由于网络原因,发生错误!请点击 <a href="chat.php?act=chat"><strong>重试</strong></a> ,重新连接...','content' => '' ); } $result = json_encode($result); exit($result); } /** * 用户离线 */ function action_off_line() { // 用户超过5分钟未发言则视为自动离线,更新客服状态 } function is_telephone ($phone) { $chars = "/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/"; if(preg_match($chars, $phone)) { return true; } } /** * 获取db对象 * * @return unknown */ function get_database () { return $GLOBALS['db']; } /** * 获取smarty对象 * * @return unknown */ function get_smarty () { return $GLOBALS[smarty]; } /** * 获取ecs对象 * * @return unknown */ function get_ecs () { return $GLOBALS['ecs']; } /* * 获取商品所对应店铺的店铺基本信息 * @param int $suppid 店铺id * @param int $suppinfo 入驻商的信息 */ function get_dianpu_baseinfo ($suppid = 0) { if(intval($suppid) <= 0) { return; } $sql_supplier = "SELECT s.supplier_id,s.supplier_name,s.add_time,sr.rank_name FROM " . $GLOBALS['ecs']->table("supplier") . " as s left join " . $GLOBALS['ecs']->table("supplier_rank") . " as sr ON s.rank_id=sr.rank_id WHERE s.supplier_id=" . $suppid . " AND s.status=1"; $supp_info = $GLOBALS['db']->getRow($sql_supplier); $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('supplier_shop_config') . " WHERE supplier_id = " . $suppid; $shopinfo = $GLOBALS['db']->getAll($sql); $config = array(); foreach($shopinfo as $value) { $config[$value['code']] = $value['value']; } $shop_info = array(); $shop_info['ghs_css_path'] = 'themes/' . $config['template'] . '/images/ghs/css/ghs_style.css'; // 入驻商所选模板样式路径 $shoplogo = empty($config['shop_logo']) ? 'themes/' . $config['template'] . '/images/dianpu.jpg' : $config['shop_logo']; $shop_info['shoplogo'] = $shoplogo; // 商家logo $shop_info['shopname'] = htmlspecialchars($config['shop_name']); // 店铺名称 $shop_info['suppid'] = $suppid; // 商家名称 $shop_info['suppliername'] = htmlspecialchars($supp_info['supplier_name']); // 商家名称 $shop_info['userrank'] = htmlspecialchars($supp_info['rank_name']); // 商家等级 $shop_info['region'] = get_province_city($config['shop_province'], $config['shop_city']); $shop_info['address'] = $config['shop_address']; $shop_info['serviceqq'] = $config['qq']; $shop_info['serviceww'] = $config['ww']; $shop_info['serviceemail'] = $config['service_email']; $shop_info['servicephone'] = $config['service_phone']; $shop_info['createtime'] = gmdate('Y-m-d', $config['add_time']); // 商家创建时间 $suppid = (intval($suppid) > 0) ? intval($suppid) : intval($_GET['suppId']); return $shop_info; } 构建多商户人工客服系统
07-16
<?php session_start(); include "Conn/conn.php"; include "page.php"; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>博文列表</title> <link rel="stylesheet" href="CSS/list.css"/> <link rel="stylesheet" href="CSS/index.css"/> </head> <body style=" background-color:#F1F2F6;"> <div class="banner"> <div class="nav"> <a class="left" href="index.php">博客系统</a> <div class="right"> <?php if(isset($_SESSION['username'])) { ?> <a class="a" href="./myinfo.php">个人中心</a> <a class='a' href='#'> | </a> <a class="a" href="./doAction.php?a=loginout">安全退出</a> <?php }else { ?> <a class='a' href='./login.php'>登录</a> <a class='a' href='#'> | </a> <a class='a' href='./Register.php'>注册</a> <?php } ?> </div> </div> </div> <div class="bgimg"></div> <!--大盒子--> <div id="big" style="margin-top: -50px;border-radius:5px;"> <!--列表开始--> <div id="list"> <div id="listbt"> <div id="bkname"> <h4>博文列表</h4> </div> <div id="bzname"> </div> </div> <div id="bkinfo"> <div id="bklogo"> <img src="images/PHP.png"/> </div> <div id="tjinfo"> </div> <div id="bkjianj"> </div> </div> <div id="biaotou"> <ul> <li class="tiezibt" style="width: 550px;"><a href="#">标题</a></li> <li><a href="#">作者</a></li> <li><a href="#">查看</a></li> <li><a href="#">发表时间</a></li> </ul> </div> <?php //分页 $sql="select id from tb_article "; $result=mysqli_query($link,$sql); //计算总条数 $rows = $result->num_rows; //调用分页函数 Page($rows,4); $sql = "select * from tb_article limit $select_from $select_limit"; $result=mysqli_query($link,$sql); //5 if($result &amp;&amp; mysqli_num_rows($result)>0){ while($rows=mysqli_fetch_assoc($result)){ ?> <div id="listks"> <div class="invitationzt"> <div class="invitalogo"> </div> <div class="invitationbt"> <a href="#"><?php echo $rows['title']?></a> </div> <div class="custor"> <a href="#"><?php echo $rows['author']?></a> </div> <div class="reply"> <a href="./file_show.php?id=<?php echo $rows['id']?>">查看</a> </div> <div class="last"> <?php echo date("Y-m-d",$rows['time'])?> </div> </div> </div> <?php } } ?> <div id="page" style="text-align: center;"> <div id="pagefy" style="margin: 0 auto;"> <ul> <?php echo $pagenav;?> </ul> </div> </div> </div> <!--列表结束--> </div> <div id="clear"></div> <!--大盒子结束--> <!--页面尾开始--> <?php include "footer.php"; ?> <!--页面尾结束--> </body> </html>在该代码中,添加标题什么的信息,修改代码并输出
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值