MySQL数据库

Mysql存储过程

DROP PROCEDURE IF EXISTS [存储过程名];
DELIMITER [结束标记]
CREATE PROCEDURE [存储过程名]([参数1], [参数2] ...)
BEGIN
        [存储过程体(一组合法的SQL语句)]
END [结束标记]
DELIMITER ;

当存储过程有且仅有一条 SQL 语句时,BEGIN 和 END 可以省略。 存储过程中的参数可分为 3 部分,分别是 [参数模式] [参数名] [参数类型] ,比如
IN name VARCHAR[20]

参数模式也分为 3 种,如下

IN:输入参数
OUT:可以作为返回值的参数
INOUT:既可以作为输入参数,也可以作为返回值参数

DELIMITER [存储过程名] 本身与存储过程的语法无关,用于表示存储过程的结束。最后一个命令(DELIMITER ;)将分隔符更改回分号(。

调用存储过程使用CALL [存储过程名]([参数1], [参数2] …);语句。

删除存储过程使用DROP PROCEDURE IF EXISTS [存储过程名]。

删除主键的语法是:
ALTER TABLE TABLE_NAME DROP PRIMARY KEY;

create database 数据库名;

导入文本文件数据

进入mysql时如下:

set global local_infile=1;
mysql -u root -p db1 --local-infile=1;


load data local infile '/home/mxs/test.txt' into table user13 character set utf8;



插入数据

insert into 表名 value(‘value1’,‘value2’,‘vaule3’,‘value4’,……)’

代码内容

mysql常用数据类型

MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。

创建数据表

create table yg(id int,name varchar(20),birthday data);
show tables;#查看表
查看表结构
desc yg
create table yg2 like yg;#快速创建一个与已有表结构相同的新表

添加表列

alter table yg2 add address varchar(100);

修改列类型

 alter table yg2 modify id tinyint(3);
 
 +----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | tinyint(3)   | YES  |     | NULL    |       |
| name     | varchar(20)  | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| address  | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+

向表中插入数据;

insert into yg2(id,name) values(1,'zhangsan');
insert into yg2(birthday,address) values('1995-10-27','安徽省苏州市');

insert set语句
insert into yg2
set id=3
name=‘lisi’
birthday=‘1997-10-26’
address=‘江苏省昆山’;

删除表中全部数据

方法1
delete from 表名
方法2
truncate table 表名

修改表中的数据

Update 语句
Update 语句用于修改表中的数据。
语法:

UPDATE 表名称 SET 列名称 = 要改值 WHERE 条件

删除某一字段

alter table tes drop column field_name;

查询表中的所有数据
select * from yg2;
select * from yg2 where id=3;
select * from yg2 where id=1 and name=‘zhangsan’;

select * from yg2 where name like ‘%zhang%’ limit 1
#姓名像zhang,限一行

删除表
drop table 表名;

查看数据库字符集

show variables like ‘%char%’;

如何删除某一条数据

delete from student where id=‘0001’
mysql> delete from student where id=‘0001’
-> ;
Query OK, 1 row affected (0.00 sec)

修改数据
update studengt set id=‘0007’ where name=‘wangming’;

set后为修改值,where为索引值;

总结

–增加
insert
–删除
delete
–修改
update
–查询
select

约束 (数据有效性)

–主键约束
对某些字段的约束,他能够唯一确定一张表中的一条记录,也就是我们通过给某字段添加约束就可以使得该字段不重复且不为空;

create table user(id int primary key,name varchar(20));
insert into user values('001','zhangsan');  

create table user2(id int,name varchar(20),pass varchar(20),primary key(id,name));#联合主键,及主键值加起来不重复就可以。
insert into user2 values('001','zhangsan');  
insert into user2 values('002','zhangsan');  

–自增约束
和主键约束一起控制主键自动增长
primary key auto_increment

create table user3(id int primary key auto_increment,name varchar(20));
mysql> insert into user3 (name) values('zhangsan');

Query OK, 1 row affected (0.01 sec)

mysql> select * from user3;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
+----+----------+
1 row in set (0.00 sec)

mysql> insert into user3 (name) values('zhangsan');
Query OK, 1 row affected (0.00 sec)

mysql> select * from user3;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | zhangsan |
+----+----------+
2 rows in set (0.01 sec)

如果创建表时忘记创建主键约了怎么办?

create table user4(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)

mysql> desc user4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

添加约束  

alter table user4 add primary key(id);
mysql> desc user4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | 0       |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

删除约束
alter table user4 drop primary key;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc user4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | 0       |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

通过修改一个字段的方式添加主键约束

alter table user4 modify id int primary key;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

–唯一约束
约束字段值不可重复,

create table user5(id int,name varchar(20));
alter table user5 add unique(name);

mysql> desc user5
    -> ;    
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  | UNI | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

多字段加起来不重复即可

create table user8(id int,name varchar(20) unique(id,name));
mysql> create table user8(id int,name varchar(20),unique(id,name));
Query OK, 0 rows affected (0.01 sec)

mysql> desc user8;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | MUL | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

—删除唯一约束

alter table user8 drop index id;

Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user8;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)


alter table user8 modify id int(20) unique;

Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc user8;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(20)     | YES  | UNI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

总结
–建表的时候添加约束;
–可以使用alter add;
–可以使用alter modify;
–s

–约束

–外键约束
涉及两个表,一个是父表,一个是子表
create table classes(id int primary key, name varchar(20));

mysql> create table students (id int primary key, name varchar(20), class_id int, foreign key(class_id) references classes(id) );
Query OK, 0 rows affected (0.00 sec)

mysql> desc students;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| class_id | int(11)     | YES  | MUL | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

insert into classes(id,name) values(1,‘一班’);
insert into classes(id,name) values(2,‘二班’);
insert into classes(id,name) values(3,‘三班’);
insert into classes(id,name) values(4,‘四班’);

insert into students(id,name,class_id) values(1001,'zhangsan',1);
insert into students(id,name,class_id) values(1002,'zhangsan',2);
insert into students(id,name,class_id) values(1003,'zhangsan',3);
insert into students(id,name,class_id) values(1004,'zhangsan',4);

如果插入一个5班,则报错

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`db2`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))

–1 主表中没有的数据在附表中不可使用
–2 主表中的记录被附表引用是不可以被删除的
如delete from classes where id=4;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (db2.students, CONSTRAINT students_ibfk_1 FOREIGN KEY (class_id) REFERENCES classes (id))

导出数据库

mysqldump -uroot -p123j db2>db2.sql

查看大小
du -sh db2.sq

查看内容
vi db2.sql

你可能感兴趣的:(笔记,学习,mysql)