当前位置:网站首页>[MySQL database learning]
[MySQL database learning]
2022-07-06 13:53:00 【Fu Ying ('▿')】
MySQL database
1、 Connect to database :
mysql -u root -p
1234562、 Create database :
create database Database name ( In letters , Numbers , String of underscores , But don't start with a number )
3、 view the database :
show databases4、 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-8 7104 约瑟夫问题(PTA程序设计)
- 1. First knowledge of C language (1)
- 7-11 机工士姆斯塔迪奥(PTA程序设计)
- Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
- Get started with typescript
- Using spacedesk to realize any device in the LAN as a computer expansion screen
- 7-4 散列表查找(PTA程序设计)
- Implementation of count (*) in MySQL
- Mixlab unbounded community white paper officially released
- 仿牛客技术博客项目常见问题及解答(三)
猜你喜欢

Safe driving skills on ice and snow roads

Write a program to simulate the traffic lights in real life.
![[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission

深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning

7-7 7003 组合锁(PTA程序设计)

Record a penetration of the cat shed from outside to inside. Library operation extraction flag

Mortal immortal cultivation pointer-1

仿牛客技术博客项目常见问题及解答(二)
![[面试时]——我如何讲清楚TCP实现可靠传输的机制](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[面试时]——我如何讲清楚TCP实现可靠传输的机制

强化学习基础记录
随机推荐
[hand tearing code] single case mode and producer / consumer mode
实验四 数组
(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
Cookie和Session的区别
1. First knowledge of C language (1)
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
[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
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
MATLAB打开.m文件乱码解决办法
【MySQL数据库的学习】
js判断对象是否是数组的几种方式
The latest tank battle 2022 full development notes-1
canvas基础2 - arc - 画弧线
【手撕代码】单例模式及生产者/消费者模式
MySQL事务及实现原理全面总结,再也不用担心面试
【九阳神功】2017复旦大学应用统计真题+解析
Programme de jeu de cartes - confrontation homme - machine
强化学习基础记录
7-1 输出2到n之间的全部素数(PTA程序设计)
Reinforcement learning series (I): basic principles and concepts