Sqlite 如何基于其他列值设置唯一约束
我为客户域维护编写以下脚本。在我的脚本中,我想在表中修改。如果状态为“活动”,我想将唯一约束设置为端口字段。否则,如果状态为“非活动”,我不想设置唯一约束。如何根据其他列值设置此约束。。?请帮帮我Sqlite 如何基于其他列值设置唯一约束,sqlite,Sqlite,我为客户域维护编写以下脚本。在我的脚本中,我想在表中修改。如果状态为“活动”,我想将唯一约束设置为端口字段。否则,如果状态为“非活动”,我不想设置唯一约束。如何根据其他列值设置此约束。。?请帮帮我 #!/bin/bash echo " --- Enter the Database name ---" #name of the database read databasename echo " --- enter the table name --- " #name of the ta
#!/bin/bash
echo " --- Enter the Database name ---" #name of the database
read databasename
echo " --- enter the table name --- " #name of the table
read table_name
sqlite3 $databasename.db "DROP TABLE IF EXISTS $table_name;"
sqlite3 $databasename.db "CREATE TABLE IF NOT EXISTS $table_name(cus_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,cus_name TEXT NOT NULL UNIQUE ,cus_domain TEXT UNIQUE,Created_on default CURRENT_DATE,cus_status TEXT NOT NULL,Port INTEGER NOT NULL);" #table creation
echo " --- Enter the total number of customer records do you want ---"
read cus_count # number of rows value
echo "--- Enter the following details one by one---"
RED='\033[0;31m'
NC='\033[0m'
port_num=8080
declare -a customer
for((i=1;i<=cus_count;i++))
do
echo "enter the $i customer details"
echo "---Enter the customer name---"
read c_name
d_name=${c_name,,}
#echo $d_name
customer=$(sqlite3 $databasename.db "select cus_domain from $table_name where cus_domain like '$d_name';")
for cus in "${customer[@]}"
do
if [[ $d_name != $customer ]];
then
echo "---Enter the Status(Active/Inactive)---"
read c_status
if [[ "$port_num" == "$port_num" ]]; then
port_num=$(($port_num + 1))
c_domain="$c_name"
sqlite3 $databasename.db "INSERT OR IGNORE INTO $table_name (cus_name,cus_domain,cus_status, Port) VALUES(\"$c_name\",\"${c_domain,,}\",\"${c_status,,}\",\"$port_num\") ;"
fi
else
echo -e "${RED}!!!OOPS for you entered customer name already domain name assigned!!!${NC}"
echo -e "${RED}Please enter new customer name${NC}"
i=$(($i - 1))
fi
done
done
echo " --- Records from the $table_name ---"
sqlite3 $databasename.db "select * from $table_name;"
#/bin/bash
echo“---输入数据库名称--”#数据库名称
读取数据库名
echo“---输入表名--”#表名
读取表名称
sqlite3$databasename.db“删除表(如果存在)$TABLE\u name;”
sqlite3$databasename.db“创建表如果不存在$TABLE_名称(cus_id整数主键自动递增唯一、cus_名称文本不为NULL唯一、cus_域文本不为NULL唯一、在默认当前_日期创建、cus_状态文本不为NULL、端口整数不为NULL);”表创建
echo“--输入您想要的客户记录总数--”
读取CUU计数#行数值
echo“---逐个输入以下详细信息--”
红色='\033[0;31m'
NC='\033[0m'
端口号=8080
申报-客户
对于((i=1;i使用:
部分索引定义可能包含UNIQUE关键字。如果包含UNIQUE关键字,则SQLite要求索引中的每个条目都是唯一的。这提供了一种跨表中某些行子集强制执行唯一性的机制
因此,您希望在不使用唯一约束的情况下进行一些更改。然后,您希望开始使用唯一约束。如果违反该约束,您希望发生什么?如果无法发生,那么为什么要临时禁用该约束?对于新输入的客户详细信息,我仅使用非活动状态端口号我想它是我想要的,我想它是我想要的,我想它是我想它是我想它的。YunNOSCCH1 | AJAJAX(Ajax)Ajax(Ajax)Ajax(Ajax)Ajax(Ajax)Ajax(Ajax)Ajax(Ajax)2017-08-2008-18-18-18-18-18-18-18-19(活跃)积极(活动)活跃,80co2 2(VVCOE)80COE(维维维维维维考(VCOE)VCOE)维维维维维维考(VCOE)VCOE)维维维维考(VCOE)维维维维维维考(VCOE)维维考(2017-2017-2017-2017-2017-18-18-18-18-18-18-18-18-18-18-18-18-18-18-18-18-18-18-18-18-124;有效| 8062 2|VVCOE | VVCOE | 2017-08-19 |非活动| 8063 3 | vvm | vvm | 2017-08-19 |非活动| 8063 4 | ns7 | 2017-08-19 |活动| 8064
CREATE UNIQUE INDEX i ON MyTable(Port) WHERE cus_status = 'Active';