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;