1、声明主键的方法:
您可以在创建表的时候就为表加上主键,如:
CREATE TABLE tbl_name ([字段描述省略...], PRIMARY KEY(index_col_name));
也可以更新表结构时为表加上主键,如:
ALTER TABLE tbl_name ADD PRIMARY KEY (index_col_name,…); /* 创建一个qq表,将qq_id设为主键,且没有对其进行NOT NULl约束 */ create table qq( qq_id int(10), nick_name varchar(255) not null, primary key (qq_id)) /* 插入一条数据,将qq号设为10000(咱也幻想一下),昵称设为"simaopig" */ INSERT INTO qq( qq_id, nick_name ) VALUES ( '10000', 'simaopig');
主键被认为是NOT NULL和UNIQUE约束最好的结合。如果这些列没有被明确地定义为NOT NULL,MySQL会隐含地定义这些列。
2、主键也是索引:
刚才已经说了,主键其实也是索引,甚至在MySQL的术语里面“键”就等于“索引”,所以“外键”一定要先设为“索引”。所以主键也应该和索引一样,既可以作用于单独的字段,又可以作用于多个字段。
举个简的例子吧,我住3单元,501室,我叫小小子,那么只有3单元501室才能在本小区表里面唯一确定我家。因为2单元,501室住着的可能也是个小小子,所以只有两个字段才能唯一确定我,也就是说可以二者组合作为主键。组合的主键,每个列都会隐含定义NOT NULL约束,且其二者加在一起被定义了UNIQUE 惟一约束。
/* 创建防火墙表,将host 和port组合设为主键,注意我没有将port设NOT NULL约束 */ create table firewall( host varchar(11) not null, port smallint(4), access enum('deny', 'allow') not null, primary key (host,port)) /* 插入一条新的记录,没有啥问题 1 row(s) inserted. */ INSERT INTO firewall ( host , port , access) VALUES ( '202.65.3.87', '21', 'deny');
3、设置主键自增
下面我们通过一个实例来讲解设置主键自增的方法:
首先创建数据库,创建表
mysql> create database ssh2;
Query OK, 1 row affected (0.04 sec)
mysql> use ssh2;
Database changed
mysql> create table user( -> id integer primary key, -> firstname varchar(200) not null, -> lastname varchar(200) not null, -> age integer -> );
Query OK, 0 rows affected (0.46 sec)
给主键增加一个自增的功能:
mysql> alter table user modify id integer auto_increment ;
Query OK, 1 row affected (0.28 sec) Records: 1 Duplicates: 0 Warnings: 0
这样,上面的user表里面的主键,id可以自增了。
给上面的主键id增加默认值和自增功能。
mysql> alter table user modify id integer auto_increment ;
Query OK, 0 rows affected (0.39 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user modify id integer default '1';
Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user modify id integer auto_increment ;
Query OK, 1 row affected (0.28 sec) Records: 1 Duplicates: 0 Warnings: 0
MySql获取系统时间:
mysql> alter table user add createtime timestamp default current_timestamp;
Query OK, 2 rows affected (0.17 sec) Records: 2 Duplicates: 0 Warnings: 0
MySql设置主键不能为空,还要自动增长(这里没有设置默认值,但是默认是1,从1开始增长。),还要得到系统默认日期:
mysql> create table dd( -> id int primary key not null auto_increment, -> name varchar(20), -> time timestamp default current_timestamp -> );
Query OK, 0 rows affected (0.10 sec)
mysql> insert into dd(name) values ('fhihgifds');
Query OK, 1 row affected (0.14 sec)
mysql> insert into dd(name) values ('steven');
Query OK, 1 row affected (0.08 sec)
mysql> select * from dd;
+----+-----------+---------------------+ | id | name | time | +----+-----------+---------------------+ | 1 | fhihgifds | 2011-03-27 01:58:46 | | 2 | steven | 2011-03-27 01:59:35 | +----+-----------+---------------------+ 2 rows in set (0.08 sec)
mysql> insert into dd(name) values ('anthony');
Query OK, 1 row affected (0.09 sec)
mysql> select * from dd;
+----+-----------+---------------------+ | id | name | time | +----+-----------+---------------------+ | 1 | fhihgifds | 2011-03-27 01:58:46 | | 2 | steven | 2011-03-27 01:59:35 | | 3 | anthony | 2011-03-27 02:00:07 | +----+-----------+---------------------+ 3 rows in set (0.00 sec)

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.