add constraint pk_StuNo primary key(StudentNo) 1. 2. 3. --给身份证添加唯一约束-- 代码如下: alter table Student add constraint uq_StuIdcard unique(IDENTITYcard) 1. 2. ---给地址address添加默认约束-- 代码如下: alter table Student add constraint df_stuaddress default('地址不详') for Addre...
`gradename` VARCHAR(50) NOT NULL COMMENT '年级名称', PRIMARY KEY(`gradeid`) )ENGINE=INNODB DEFAULT CHARSET=utf8 -- 创建student表 CREATE TABLE IF NOT EXISTS `student`( `id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '学号', `name` VARCHAR(20) NOT NULL DEFAULT '匿名' COMMENT '姓名...
ALTER TABLE 表名ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名) REFERENCES 主表(主表字段名) ON UPDATE CASCADE ON DELETE CASCADE; 演示如下: 代码语言:sql AI代码解释 alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update cascade on delete cascade ...
* create database 数据库名称; * 创建数据库,判断不存在,再创建: * create database if not exists 数据库名称; * 创建数据库,并指定字符集 * create database 数据库名称 character set 字符集名; * 练习: 创建db4数据库,判断是否存在,并制定字符集为gbk * create database if not exists db4 characte...
alter table tablename add idintidentity(1,1) GO---设置IDENTITY列为主键 alter table tablename add constraint [PK_tablename] PRIMARY KEY CLUSTERED ([id]) 但是这样做会改变主键字段在表中的顺序 如果不想改变表中主键的位置,有两种思路,一是删除表后重建;一是不删除,将字段逐个删除再依次添加字段: 看...
if exists (select * from sysdatabases where name ='stuDB') drop database stuDB create datab 1. 2. 3. 4. 5. 6. ase stuDB go 执行语句结果是: 可以看到,左边的显示出,我们已经新建了一个名为stuDB的数据库. 2.创建及删除数据表
SQL 复制 create table table1 ( TransactionID bigint not null, UserID int not null, SomeInt int not null ); go alter table table1 add constraint pk_table1 primary key clustered (TransactionID, UserID); go 备注 表定义中的对象名称已从原始值更改为其他值。
USE WideWorldImporters; GO SET NOCOUNT ON; IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Warehouse.StockItemTransactions_bcp') BEGIN SELECT * INTO WideWorldImporters.Warehouse.StockItemTransactions_bcp FROM WideWorldImporters.Warehouse.StockItemTransactions WHERE 1 = 2; ALTER TABLE Warehouse...
whereagein(13,15);-- 或者用where age>=13 and age<=15,注意between包含边界;或者用where age>=13 && age<=15select*fromtablewhereagebetween13and15;-- not表示不在13到15范围的所有select*fromtablewhereagebetween13and15;-- 查询为null的select*fromtablewhereageisnull;--where age is not null;不...
DROP TABLE [ IF EXISTS ] 表名; 2). 删除指定表, 并重新创建表 TRUNCATE TABLE 表名; 注意: 在删除表的时候,表中的全部数据也都会被删除。 DML数据库操作语言 添加数据 1). 给指定字段添加数据 INSERT INTO 表名 (字段名1, 字段名2, ...) VALUES (值1, 值2, ...); 案例: 给user表所有...