当前位置:网站首页>[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;
边栏推荐
- (原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
- Miscellaneous talk on May 14
- Cookie和Session的区别
- Safe driving skills on ice and snow roads
- Canvas foundation 1 - draw a straight line (easy to understand)
- 【九阳神功】2019复旦大学应用统计真题+解析
- 扑克牌游戏程序——人机对抗
- UGUI—Text
- The latest tank battle 2022 full development notes-1
- 简单理解ES6的Promise
猜你喜欢
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
Nuxtjs quick start (nuxt2)
Leetcode.3 无重复字符的最长子串——超过100%的解法
Mortal immortal cultivation pointer-2
MySQL lock summary (comprehensive and concise + graphic explanation)
1143_ SiCp learning notes_ Tree recursion
SRC mining ideas and methods
仿牛客技术博客项目常见问题及解答(二)
Programme de jeu de cartes - confrontation homme - machine
MySQL锁总结(全面简洁 + 图文详解)
随机推荐
深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning
撲克牌遊戲程序——人機對抗
Matlab opens M file garbled solution
SRC挖掘思路及方法
[graduation season · advanced technology Er] goodbye, my student days
(original) make an electronic clock with LCD1602 display to display the current time on the LCD. The display format is "hour: minute: Second: second". There are four function keys K1 ~ K4, and the fun
稻 城 亚 丁
FAQs and answers to the imitation Niuke technology blog project (I)
Reinforcement learning series (I): basic principles and concepts
强化学习基础记录
实验八 异常处理
Analysis of penetration test learning and actual combat stage
Wechat applet
JS several ways to judge whether an object is an array
Yugu p1012 spelling +p1019 word Solitaire (string)
[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
3. C language uses algebraic cofactor to calculate determinant
1. Preliminary exercises of C language (1)
[hand tearing code] single case mode and producer / consumer mode
5月27日杂谈