当前位置:网站首页>【MySQL数据库的学习】
【MySQL数据库的学习】
2022-07-06 09:22:00 【馥滢( '▿ ' )】
MySQL数据库
1、 连接数据库:
mysql -u root -p
123456
2、 创建数据库:
create database 数据库名称(以字母,数字,下划线组成的字符串,但不要以数字开头)
3、 查看数据库:
show databases
4、 查看字符集:
show variables like ‘character%’
5、 查看端口号:
show variables like ‘port’
6、 查看数据存储路径:
show variables like ‘datadir'
MySQL数据库表操作
1、创建表
create table test(id int(11),name varchar(50));
create table student(sno char(11) primary key,sname varchar(20) not null);
2、创建表test
use demo
create table test(id int(11),name varchar(50));
3、创建一个表student
create table student(sno char(11) primary key,sname varchar(20) not null);
4、创建表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、增(为表添加新的字段)
mysql> alter table student
-> add ssex char(2) not null
-> ;
6、改(为表中字段设置默认值)
mysql> alter table student
-> alter ssex set default '男'
-> ;
7、更改表名
法1
mysql> rename table test to test2;
法2
mysql> alter table test2
-> rename to test;
8、更改student表中ssex的数据类型为枚举类型
(‘male’,'female'),默认值为‘male’
mysql> alter table student
-> modify ssex enum('male','female') not null;
9、更改已有字段的类型
mysql> alter table student
-> alter ssex set default 'male';
10、添加主键
mysql> alter table test
-> add primary key(id)
-> ;
11、添加外键
mysql> create table sc(sno char(11),
-> cno varchar(20),
-> grade decimal(6,2)
-> );
12、以多个关键字组合
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(对表结构上的操作):create alter drop
DML(表内容):insert update delete
DQL:select * from 表名 #“*”相当于投影
1、为表添加数据
insert into 表名(字段1,字段2,...) values(值1,值2,...)
mysql> insert into test(id,name) values(1,'张三');
mysql> insert into test(id) values(2);
mysql> insert into test(id,name) values(3,'li');
简写方法:省略字段部分,值需要和字段一一对应
mysql> insert into test values(4,'王五');(√)
mysql> insert into test values('王五',4);(×)
省略字段,且只给部分值
mysql> insert into test values(5,null);
一次添加多个值的方法:
mysql> insert into test(id) values(6),(7),(8),(9),(10);
mysql自有的添加记录的语句
mysql> insert into test
-> set id=11,
-> name='王美丽';
insert into 表2 select * from 表1;
mysql> create table demo like test;
mysql> insert into demo select *from test;
2、修改表记录
1、修改表中id为2行的值
mysql> update test set name='jerry' where id=2;
2、修改test表为其增加一个age列,默认值为20
mysql> alter table test
-> add age int(3) not null default 20;
3、改表中两个值用or连接
mysql> update test set age=20 where id=5 or id=7;
4、在同一个条件下修改多个值,同时改name和age
mysql> update test
-> set name='tom',
-> age=23
-> where id=6;
5、修改多个表,同时修改两个表中的id为8的name
mysql> update test,demo1
-> set test.name='秦建兴',demo1.name='秦建兴'
-> where test.id=8 and test.id=demo1.id;
6、删除记录
mysql> delete from test where id=10; #回收
Truncate table 表名 /*彻底删除,删除数据不可恢复
补充:replace删除了一个数据并送入表中一个数据 /*安全性相对较低
mysql> replace into test(id,name,age) values(1,'王五',24);
MySQL索引与完整性约束
mysql> create table test2(id int primary key);#列级约束
mysql> create table test3(id int,primary key(id));#表级约束
1、创建普通索引(属性值可以重复)
mysql> create index idx_name on test(name) ;
mysql> desc test;
mysql> show index from test;
- 创建唯一索引(属性值不能重复,比较适合候选码)
mysql> create unique index idx_age on test(age);
3、表不存在时,可以创建为:
mysql> create table test4(id int,name varchar(10),
-> primary key(id),
-> unique(name) #建表的同时创建唯一索引
-> );
mysql> insert into test4 values(1,'tom');(√)
mysql> insert into test4 values(2,'tom');(×)
比较几种索引
- 一个表只能创建一个的索引是:主键(索引)
- 一个表可以创建多个普通或者唯一索引
- 创建为索引的属性列值必须唯一的是:主索引和唯一索引,值可以重复的是普通索引
为索引命名
mysql> create table test5(id int,name varchar(10),
-> constraint mypri primary key(id),
-> constraint myuiq unique(name)
-> );
删除索引(可以更加索引名称,进行删除操作):
删除主键(索引),不需要使用名称
mysql> alter table test5 drop primary key;
删除其他索引,需要使用名称
mysql> alter table test5 drop index myuiq;
或者是
mysql> drop index myuiq on test5;
边栏推荐
猜你喜欢
Programme de jeu de cartes - confrontation homme - machine
FAQs and answers to the imitation Niuke technology blog project (I)
canvas基础2 - arc - 画弧线
仿牛客技术博客项目常见问题及解答(二)
SRC mining ideas and methods
2. First knowledge of C language (2)
C语言入门指南
7-7 7003 组合锁(PTA程序设计)
撲克牌遊戲程序——人機對抗
The latest tank battle 2022 - Notes on the whole development -2
随机推荐
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
[modern Chinese history] Chapter 9 test
Read only error handling
渗透测试学习与实战阶段分析
Wechat applet
[面試時]——我如何講清楚TCP實現可靠傳輸的機制
Matlab opens M file garbled solution
Record a penetration of the cat shed from outside to inside. Library operation extraction flag
【数据库 三大范式】一看就懂
String abc = new String(“abc“),到底创建了几个对象
4. Branch statements and loop statements
ArrayList的自动扩容机制实现原理
6. Function recursion
Safe driving skills on ice and snow roads
The difference between cookies and sessions
Relationship between hashcode() and equals()
Poker game program - man machine confrontation
Get started with typescript
Detailed explanation of redis' distributed lock principle
【VMware异常问题】问题分析&解决办法