search
HomeDatabaseMysql TutorialTutorial on operating tables in mysql

Creation and deletion of database
Start the database service in a black window: net start mysql
Close the database service: net stop mysql

Create database
Use keyword create database
Format:
create database database name;
create database database name character set character set;

View all databases in mysql
show databases;

View the definition information of a database
show create database database name Example: show create database mybase;

Switch database
use database name Example: use test;

View the database in use
select database;

Delete database
drop database database name Example: drop database test;

Create a table in the database.

Use the keyword create table
[] It means optional in the database. It can be present or not.
Format:
create table table name (
 Field name data type [length] [constraint],
 Field name data type [length] ] [Constraint],
……
Field name data type [length] [Constraint](The last one cannot have a comma)
);
Example: Create a product classification table category
create table category(
 cid int primary key,
 cname varchar(100)
);

View all tables in the current database
show tables;

View table structure
desc table name Example: desc category;


Delete table
Format: drop table table name
Example: drop table category;

Modify table to add columns
alter table table nameadd column name type [length] [constraint];
Example: alter table category add name int;

Modify table to modify column type length and constraints
alter table table name modify column name type [length] [constraint];
Note: If there is data, you must pay attention to the data type varchar--> it is easy to have wrong data
Example: alter table category modify description int;
alter table category modify description varchar(20) not null;


Modify column names, data types and constraints
alter tble table name drop column name;
Note: If there is data in the column, it will be deleted together, so be careful
Example: alter table category drop descr;


Modify table name
rename table table name to new table name
Example:rename table category to student;

Modify the character set of the table
alter table table name character set character set
Note: It is not recommended to execute as it may produce garbled characters
Example: alter table category character set gbk;


Insert data into the database table
Use the keyword insert [into]
Format:
Contains the primary key: insert into table name ( Field 1, field 2,....) values ​​(value 1, value 2,....);
The primary key is incremented, the primary key is omitted: insert into table name (excluding primary key) values ​​(excluding primary key) ;
Notes:
1. Fields and values ​​must correspond one-to-one (number, data type)
2. In addition to numerical types (int, double), other data Types need to be wrapped in quotes
You can use ''. You can also use "", it is recommended to use ''
Contains the primary key: insert into table name (field 1, field 2,...) values ​​(value 1 , value 2,....);
Example: insert into category (cid,cname) values ​​(1,"clothing");
insert into category (cid,cname) values ​​(1,"color TV" ; 100)
);
The primary key is automatically incremented, and the primary key is omitted: insert into table name (excluding primary key) values ​​(excluding primary key);
Example: insert into category (cname) values ​​("Color TV") ;




Batch insert data

Format:

Contains primary key: insert into table name (Field 1, Field 2,...) values ​​( Value 1, Value 2,...), (Value 1, Value 2,...), (Value 1, Value 2,...);

The primary key is incremented, the primary key is omitted: insert into table name ( Does not include primary key) values ​​(value 1, value 2,...), (value 1, value 2,...)..;

insert into category (cid,cname) values ​​(3,'air conditioner') ,(4,'Washing machine');insert into category (cname) values ​​('Microwave oven'),('Induction cooker');


Omit field name format: must be given Out the values ​​of all fields (including primary keys)

Format:

insert into table name values ​​(values ​​of all fields);

insert into table name values ​​(values ​​of all fields), (all fields value),..;
Example: insert into category values(7,'refrigerator'); insert into category values(8,'laptop'),('desktop');


When adding data, if you cannot remember the primary key, you can use null, and sql will automatically calculate the primary key

Example: insert into category values ​​(null,'Xiaomi 6');

Update table data, use the keyword update (update, modify) set (set)
Format:
Without conditional filtering, modify all the data in the column at one time
update Table name set field name = field value, field name = field value,...;
With conditional filtering, use the keyword where
update table name set field name = field value, field name = field value,. ..where filter condition;
No low condition filtering (use with caution)
Example: update category set cname='all modifications';
With conditional filtering, use the keyword where
update category set cname ='Black and White TV' where cid=4;


To delete table data, use the keyword delete from
Format:
delete from table name[where conditional filtering ];
delete from table name deletes all data in the table, but does not delete the primary key increment
truncate table table name; deletes all data in the table, deletes the primary key increment, and resets the primary key increment Starting from 1
delete from table name[where condition filtering];
Example:delete from category where cid=4;
delete from table name
Example:delete from category;
Use delete When data is inserted after deletion, the primary key will have a broken number and no previous serial number
insert into category (cname) values ​​('mobile phone');
delete from category where cid=12;
insert into category (cid ,cname) values(12,'Manually insert the specified primary key column');

truncate table table name
Example: truncate table category


Primary key constraint
Use the key primary key
Function:
The constraint primary key column cannot be null
cannot be repeated
Each table must have a primary key, and there can only be one primary key
Primary key Business data cannot be used


The first way to add a primary key
Add directly after the column name
create table persons(
 Id_p int primary key,
 LastName varchar(255),
 FirstName varchar(255),
 Address varchar(255),
 City varchar(255)
);
insert into persons(Id_p,LastName ) values ​​(1,'Zhang');
insert into persons(Id_p,LastName) values ​​(null,'Zhang');-- non-null
insert into persons(Id_p,LastName) values ​​(1,' Zhang');-- Repeat


The second way to add a primary key
Use the constraint area
Format:
[constraint name] primary key ( Field list)
CREATE TABLE persons(
Id_P INT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255 ),
 CONSTRAINT pk_id_p PRIMARY KEY(Id_P)
);
constraintIf the name of the primary key is not given, the keyword constraint can be omitted
CREATE TABLE persons(
 Id_P INT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255),
PRIMARY KEY(Id_P)
);


The third way to add a primary key
After creating the table, modify the table structure, the first way to add a primary key
alter table table name add [constraint name] primary key (field list);
CREATE TABLE persons(
Id_P INT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
);
ALTER TABLE persons ADD PRIMARY KEY(Id_P);


Delete primary key
alter table persons drop primary key;


Joint primary key
Use more than two fields as the primary key
CREATE TABLE persons(
Id_P INT,
LastName VARCHAR(255),
​FirstName VARCHAR(255),
​Address VARCHAR(255),
​City VARCHAR(255),
​PRIMARY KEY(LastName,FirstName)
);


Non-null constraint
Use the keyword not null
Function: Force the constraint that a certain column cannot be null (null values ​​are not accepted)

Create the first step of the non-null constraint A format that creates a representation and gives directly after the field
CREATE TABLE persons(
Id_P INT PRIMARY KEY AUTO_INCREMENT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255) NOT NULL
);
Add data
INSERT INTO persons(lastname,city) VALUES('Zhang','Xiongxian') ;
INSERT INTO persons(lastname,city) VALUES('李','null');
INSERT INTO persons(lastname,city) VALUES('王','');
INSERT INTO persons (lastname,city) VALUES('Zhao',NULL);-- Column 'City' cannot be null


java Four are empty
String s ="";s ="null" s=null; void

Create non-null constraint method two
Modify table structure
alter table table name modify column name type [length] [constraint];
CREATE TABLE persons(
 Id_P INT PRIMARY KEY AUTO_INCREMENT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
);
ALTER TABLE persons MODIFY city VARCHAR(255)NOT NULL;

Delete non-null constraints
alter table persons modify city varchar(255);

Unique constraint
Use the keyword unique
Function: The field with the unique constraint cannot be repeated

The first format to create a unique constraint, create a table When,
CREATE TABLE persons(
Id_P INT PRIMARY KEY AUTO_INCREMENT,
is given directly after the field) LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR (255),
 City VARCHAR(255) UNIQUE
);
INSERT INTO persons (lastname,city) VALUES('Zhang','Mauritius');
-- Duplicate entry 'Mauritius' for key 'City'
INSERT INTO persons (lastname,city) VALUES('王','Mauritius');


Create the second format of unique constraints and create a table Use [constraint name] unique (field list)
CREATE TABLE persons(
Id_P INT PRIMARY KEY AUTO_INCREMENT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255),
CONSTRAINT UNIQUE(City)
);

Create the third format of unique constraints, after creating the table ,Modify table data
alter table table name modify column name type [length] [constraint];
CREATE TABLE persons(
 Id_P INT PRIMARY KEY AUTO_INCREMENT,
 LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
);
ALTER TABLE persons MODIFY city VARCHAR(255) UNIQUE;
alter table Table name add [constraint name] unique (field list)
CREATE TABLE persons(
Id_P INT PRIMARY KEY AUTO_INCREMENT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
 City VARCHAR(255)
);
ALTER TABLE persons ADD UNIQUE(City);


Delete unique constraint
alert table persons drop index name
When defining a constraint, if no name is created, the name is a string
alter table persons drop index city;


Default constraint
Add a default value to the field. If the field does not insert a value, use the default value
Use the keyword default value
Create default constraint method 1, create the table, and the column data type is followed by default 'default value '
CREATE TABLE persons(
Id_P INT PRIMARY KEY AUTO_INCREMENT,
LastName VARCHAR(255),
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR( 255)DEFAULT 'China'
);
INSERT INTO persons (lastname) VALUES('Zhang');
INSERT INTO persons (lastname,city) VALUES('Zhang','Canada');

The above is the detailed content of Tutorial on operating tables in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool