当前位置:网站首页>[MySQL database learning]
[MySQL database learning]
2022-07-06 13:53:00 【Fu Ying ('▿')】
MySQL database
1、 Connect to database :
mysql -u root -p
123456
2、 Create database :
create database Database name ( In letters , Numbers , String of underscores , But don't start with a number )
3、 view the database :
show databases
4、 View character set :
show variables like ‘character%’
5、 View port number :
show variables like ‘port’
6、 View the data storage path :
show variables like ‘datadir'
MySQL Database table operation
1、 Create table
create table test(id int(11),name varchar(50));
create table student(sno char(11) primary key,sname varchar(20) not null);
2、 Create table test
use demo
create table test(id int(11),name varchar(50));
3、 Create a table student
create table student(sno char(11) primary key,sname varchar(20) not null);
4、 Create table course
mysql> create table course
-> (
-> cno varchar(20) primary key,
-> course_name varchar(50) not null,
-> cpno varchar(20),
-> course_credit decimal(4,1)
-> );
5、 increase ( Add new fields to the table )
mysql> alter table student
-> add ssex char(2) not null
-> ;
6、 Change ( Set default values for fields in the table )
mysql> alter table student
-> alter ssex set default ' male '
-> ;
7、 Change table name
Law 1
mysql> rename table test to test2;
Law 2
mysql> alter table test2
-> rename to test;
8、 change student In the table ssex The data type of is enumeration type
(‘male’,'female'), The default value is ‘male’
mysql> alter table student
-> modify ssex enum('male','female') not null;
9、 Change the type of the existing field
mysql> alter table student
-> alter ssex set default 'male';
10、 Add primary key
mysql> alter table test
-> add primary key(id)
-> ;
11、 Add foreign keys
mysql> create table sc(sno char(11),
-> cno varchar(20),
-> grade decimal(6,2)
-> );
12、 Combine with multiple keywords
mysql> alter table sc
-> add foreign key(sno) references student(sno);
mysql> alter table sc
-> add foreign key(cno) references course(cno);
mysql> alter table sc
-> add primary key(sno,cno);
DDL( Operations on table structure ):create alter drop
DML( Table content ):insert update delete
DQL:select * from Table name #“*” Equivalent to projection
1、 Add data to the table
insert into Table name ( Field 1, Field 2,...) values( value 1, value 2,...)
mysql> insert into test(id,name) values(1,' Zhang San ');
mysql> insert into test(id) values(2);
mysql> insert into test(id,name) values(3,'li');
Shorthand method : Omit the field section , Values need to correspond to fields one by one
mysql> insert into test values(4,' Wang Wu ');(√)
mysql> insert into test values(' Wang Wu ',4);(×)
Omit fields , And only give part of the value
mysql> insert into test values(5,null);
Method of adding multiple values at a time :
mysql> insert into test(id) values(6),(7),(8),(9),(10);
mysql Own statements to add records
mysql> insert into test
-> set id=11,
-> name=' Wang Meili ';
insert into surface 2 select * from surface 1;
mysql> create table demo like test;
mysql> insert into demo select *from test;
2、 Modification table record
1、 Modify the table id by 2 It's worth it
mysql> update test set name='jerry' where id=2;
2、 modify test Add a age Column , The default value is 20
mysql> alter table test
-> add age int(3) not null default 20;
3、 Change the two values in the table to use or Connect
mysql> update test set age=20 where id=5 or id=7;
4、 Modify multiple values under the same condition , Simultaneous change name and age
mysql> update test
-> set name='tom',
-> age=23
-> where id=6;
5、 Modify multiple tables , Modify both tables at the same time id by 8 Of name
mysql> update test,demo1
-> set test.name=' Qin Jianxing ',demo1.name=' Qin Jianxing '
-> where test.id=8 and test.id=demo1.id;
6、 Delete record
mysql> delete from test where id=10; # Recycling
Truncate table Table name /* Delete completely , Deleted data cannot be recovered
Add :replace Delete a data and send it into the table /* Relatively low security
mysql> replace into test(id,name,age) values(1,' Wang Wu ',24);
MySQL Indexing and integrity constraints
mysql> create table test2(id int primary key);# Column level constraints
mysql> create table test3(id int,primary key(id));# Table level constraints
1、 Create a normal index ( Attribute values can be repeated )
mysql> create index idx_name on test(name) ;
mysql> desc test;
mysql> show index from test;
- Create unique index ( Attribute values cannot be repeated , More suitable for candidate code )
mysql> create unique index idx_age on test(age);
3、 When the table does not exist , Can be created as :
mysql> create table test4(id int,name varchar(10),
-> primary key(id),
-> unique(name) # Create a unique index while creating a table
-> );
mysql> insert into test4 values(1,'tom');(√)
mysql> insert into test4 values(2,'tom');(×)
Compare several indexes
- Only one index can be created for a table : Primary key ( Indexes )
- A table can create multiple ordinary or unique indexes
- The attribute column value created as an index must be unique : Primary index and unique index , The values that can be repeated are ordinary indexes
Name the index
mysql> create table test5(id int,name varchar(10),
-> constraint mypri primary key(id),
-> constraint myuiq unique(name)
-> );
Delete index ( You can index names more , Delete operation ):
Delete primary key ( Indexes ), There is no need to use a name
mysql> alter table test5 drop primary key;
Delete other indexes , Name required
mysql> alter table test5 drop index myuiq;
Or is it
mysql> drop index myuiq on test5;
边栏推荐
- Implementation of count (*) in MySQL
- canvas基础1 - 画直线(通俗易懂)
- 撲克牌遊戲程序——人機對抗
- [the Nine Yang Manual] 2016 Fudan University Applied Statistics real problem + analysis
- MATLAB打开.m文件乱码解决办法
- 自定义RPC项目——常见问题及详解(注册中心)
- About the parental delegation mechanism and the process of class loading
- 优先队列PriorityQueue (大根堆/小根堆/TopK问题)
- 【数据库 三大范式】一看就懂
- Mixlab unbounded community white paper officially released
猜你喜欢
这次,彻底搞清楚MySQL索引
Safe driving skills on ice and snow roads
7-5 走楼梯升级版(PTA程序设计)
强化学习系列(一):基本原理和概念
Mixlab unbounded community white paper officially released
This time, thoroughly understand the MySQL index
关于双亲委派机制和类加载的过程
2022 Teddy cup data mining challenge question C idea and post game summary
9. Pointer (upper)
扑克牌游戏程序——人机对抗
随机推荐
Detailed explanation of redis' distributed lock principle
附加简化版示例数据库到SqlServer数据库实例中
记一次猫舍由外到内的渗透撞库操作提取-flag
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
自定义RPC项目——常见问题及详解(注册中心)
Mixlab unbounded community white paper officially released
Canvas foundation 1 - draw a straight line (easy to understand)
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
Mortal immortal cultivation pointer-2
Poker game program - man machine confrontation
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
js判断对象是否是数组的几种方式
Brief introduction to XHR - basic use of XHR
hashCode()与equals()之间的关系
SRC挖掘思路及方法
实验五 类和对象
UGUI—Text
[the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
实验七 常用类的使用