当前位置:网站首页>[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;
边栏推荐
- FAQs and answers to the imitation Niuke technology blog project (III)
- Safe driving skills on ice and snow roads
- 7-4 散列表查找(PTA程序设计)
- [modern Chinese history] Chapter V test
- Implementation principle of automatic capacity expansion mechanism of ArrayList
- 9. Pointer (upper)
- Simply understand the promise of ES6
- 杂谈0516
- SRC挖掘思路及方法
- [the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
猜你喜欢
强化学习系列(一):基本原理和概念
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
4. Branch statements and loop statements
This time, thoroughly understand the MySQL index
这次,彻底搞清楚MySQL索引
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
Caching mechanism of leveldb
受检异常和非受检异常的区别和理解
Mixlab unbounded community white paper officially released
随机推荐
【Numpy和Pytorch的数据处理】
Implementation principle of automatic capacity expansion mechanism of ArrayList
【九阳神功】2016复旦大学应用统计真题+解析
[dark horse morning post] Shanghai Municipal Bureau of supervision responded that Zhong Xue had a high fever and did not melt; Michael admitted that two batches of pure milk were unqualified; Wechat i
渗透测试学习与实战阶段分析
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
Differences among fianl, finally, and finalize
Mortal immortal cultivation pointer-2
The latest tank battle 2022 - full development notes-3
实验九 输入输出流(节选)
Inaki Ading
Reinforcement learning series (I): basic principles and concepts
JS several ways to judge whether an object is an array
Nuxtjs quick start (nuxt2)
2022 Teddy cup data mining challenge question C idea and post game summary
TypeScript快速入门
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
使用Spacedesk实现局域网内任意设备作为电脑拓展屏
记一次猫舍由外到内的渗透撞库操作提取-flag
This time, thoroughly understand the MySQL index