当前位置:网站首页>【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;
边栏推荐
猜你喜欢
Principles, advantages and disadvantages of two persistence mechanisms RDB and AOF of redis
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
About the parental delegation mechanism and the process of class loading
仿牛客技术博客项目常见问题及解答(一)
扑克牌游戏程序——人机对抗
1.初识C语言(1)
Nuxtjs快速上手(Nuxt2)
MySQL锁总结(全面简洁 + 图文详解)
8. C language - bit operator and displacement operator
Differences among fianl, finally, and finalize
随机推荐
7. Relationship between array, pointer and array
【手撕代码】单例模式及生产者/消费者模式
Redis实现分布式锁原理详解
3. Number guessing game
1. C language matrix addition and subtraction method
记一次猫舍由外到内的渗透撞库操作提取-flag
实验六 继承和多态
Inaki Ading
MySQL lock summary (comprehensive and concise + graphic explanation)
Record a penetration of the cat shed from outside to inside. Library operation extraction flag
4. Branch statements and loop statements
Principles, advantages and disadvantages of two persistence mechanisms RDB and AOF of redis
【九阳神功】2018复旦大学应用统计真题+解析
A piece of music composed by buzzer (Chengdu)
[graduation season · advanced technology Er] goodbye, my student days
Caching mechanism of leveldb
Implementation principle of automatic capacity expansion mechanism of ArrayList
5月14日杂谈
It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan
Wechat applet