当前位置:网站首页>[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;
边栏推荐
- 7-11 机工士姆斯塔迪奥(PTA程序设计)
- 深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning
- 1143_ SiCp learning notes_ Tree recursion
- .Xmind文件如何上传金山文档共享在线编辑?
- MATLAB打开.m文件乱码解决办法
- canvas基础1 - 画直线(通俗易懂)
- [graduation season · advanced technology Er] goodbye, my student days
- 3. C language uses algebraic cofactor to calculate determinant
- 仿牛客技术博客项目常见问题及解答(二)
- Service ability of Hongmeng harmonyos learning notes to realize cross end communication
猜你喜欢
Mortal immortal cultivation pointer-1
实验六 继承和多态
强化学习基础记录
About the parental delegation mechanism and the process of class loading
Leetcode. 3. Longest substring without repeated characters - more than 100% solution
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
SRC mining ideas and methods
Nuxtjs快速上手(Nuxt2)
Relationship between hashcode() and equals()
canvas基础1 - 画直线(通俗易懂)
随机推荐
【手撕代码】单例模式及生产者/消费者模式
Simply understand the promise of ES6
Package bedding of components
[the Nine Yang Manual] 2020 Fudan University Applied Statistics real problem + analysis
C语言入门指南
Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
Zatan 0516
Programme de jeu de cartes - confrontation homme - machine
杂谈0516
7-4 散列表查找(PTA程序设计)
【九阳神功】2019复旦大学应用统计真题+解析
Implementation of count (*) in MySQL
Using qcommonstyle to draw custom form parts
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
甲、乙机之间采用方式 1 双向串行通信,具体要求如下: (1)甲机的 k1 按键可通过串行口控制乙机的 LEDI 点亮、LED2 灭,甲机的 k2 按键控制 乙机的 LED1
Brief introduction to XHR - basic use of XHR
强化学习基础记录
Wechat applet
Matlab opens M file garbled solution