MySQL中数据的约束
今天说说数据库中的数据表数据约束问题,约束大概分为主键约束,外键约束,默认约束,唯一约束,非空约束这几种,关于讲解配置,我后面慢慢说 1、主键约束 其实是用来唯一标示表中的一个列或多个列,但是一个表中必须唯一,配置方法 (1)、初始化列级主键约
今天说说数据库中的数据表数据约束问题,约束大概分为主键约束,外键约束,默认值约束,唯一约束,非空约束这几种,关于讲解配置,我后面慢慢说
1、主键约束
其实是用来唯一标示表中的一个列或多个列,但是一个表中必须唯一,配置方法
(1)、初始化列级主键约束
create table custominfo ( id int primary key, name varchar(12), age int );
(2)、初始化表级主键约束
create table custominfo ( id int, name varchar(12), age int, primary key(id) );
(3)、修改主键约束
alter table customerinfo add constraint yueshu primary key(id);
(4)、修改添加联合主键约束
alter table customerinfo add constraint yueshu primary key(id,name);
(5)、删除主键约束
alter table customerinfo drop primary key;
2、外键约束
比如某个表B关联于表A,也就是表A中所有项目表B中都有,就可以设置表A主键关联,表B外键关联
(1)初始化添加
create table typeinfo ( typeid int primary key, type varchar(20) ); create table roominfo ( roomid int primary key, type varchar(20), price int constraint yueshu foreign key(roomid) references typeinfo(typeid) );
(2)、修改添加
alter table roominfo add constraint yueshu foreign key(roomid) references typeinfo(typeid);
(3)、删除外键约束
alter table roominfo drop foreign key yueshu;
3、默认值约束
将某一数据初始化为固定值
(1)、初始化设置
create table roominfo ( id int, name varchar(20), price int default 0 );
(2)、修改添加设置
alter table roominfo alter price set default 0;
(3)、删除默认约束
alter table roominfo alter price drop default;
4、非空约束
某项数据不得为空的约束
(1)、初始化设置
create table roominfo ( id int not null, name varchar(20) );
(2)、修改设置
alter table roominfo alter id int not null;
5、检查约束
就是固定行规定数据的范围
(1)、初始化列级设置
<pre name="code" class="sql"><pre name="code" class="sql" style="background-color: rgb(255, 255, 255); "><pre name="code" class="sql">create table roominfo ( id int, name varchar(20), price int check(price>=0) );
(2)、初始化表级设置
<pre name="code" class="sql">create table roominfo ( id int, name varchar(20), price int, check(price>=0) );
(3)、修改设置
alter table roominfo add constraint yueshu check(price>=0);
6、唯一约束
(1)、初始化列级唯一约束
create table roominfo ( id int unique, name varchar(20) );
(2)、初始化表级唯一约束
create table roominfo ( id int, name varchar(20), unique(id) );
可以设置多个独立唯一约束
(3)、初始化创建共同唯一约束
create table roominfo ( id int, name varchar(20), price int, unique(id,name) );
(4)、修改设置独立唯一约束
alter table roominfo add constraint yueshu unique(id);
(5)、修改设置共同唯一约束
alter table roominfo add constraint yueshu unqiue(id,name);
(6)、删除唯一约束
drop index yueshu on roominfo;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The best cryptocurrency trading and analysis platforms include: 1. OKX: the world's number one in trading volume, supports multiple transactions, provides AI market analysis and on-chain data monitoring. 2. Binance: The world's largest exchange, providing in-depth market conditions and new currency first-time offerings. 3. Sesame Open Door: Known for spot trading and OTC channels, it provides automated trading strategies. 4. CoinMarketCap: an authoritative market data platform, covering 20,000 currencies. 5. CoinGecko: Known for community sentiment analysis, it provides DeFi and NFT trend monitoring. 6. Non-small account: a domestic market platform, providing analysis of linkage between A-shares and currency markets. 7. On-chain Finance: Focus on blockchain news and update in-depth reports every day. 8. Golden Finance: 24 small

The top ten digital virtual currency apps are: 1. OKX, 2. Binance, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. KuCoin, 8. Bitfinex, 9. Bitstamp, 10. Poloniex. These exchanges are selected based on factors such as transaction volume, user experience and security, and all provide a variety of digital currency trading services and an efficient trading experience.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.
