2020.8.12课堂笔记(MySQL增删改查)

#切换到MySchool数据库
mysql> use MySchool
Database changed
mysql> show tables;

#desc查看表结构的详细信息 语法:desc table_name;
mysql> desc test_def;

#删除列,语法:ALTER TABLE table_name DROP COLUMN column_name
mysql> alter table test_def drop column t_m;

#增加列,语法:ALTER TABLE table_name ADD COLUMN column_name datatype(length)
mysql> alter table test_def add column t_m varchar(10) not null;
mysql> desc test_def;

#在t_b列后面插入t_c列
mysql> alter table test_def add column t_c varchar(10) not null after t_b;
mysql> desc test_def;

#重命名表 语法:ALTER TABLE table_name RENAME TO table_name;
mysql> alter table test_def rename to testdef;
mysql> desc test_def;
mysql> desc testdef;

mysql> alter table testdef rename to test_def;
#修改原字段名称及类型
#①mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
#②mysql> ALTER TABLE table_name MODIFY field_name field_type;
mysql> alter table test_def modify t_m int default 2;
mysql> desc test_def;

mysql> alter table test_def change t_m t_x double default 2;
mysql> desc test_def;

alter在实际工作当中用的不多,数据库设计,一旦定义好不会做改变的,但是这种方式要知道
mysql> desc subject;
±------------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------------±------------±-----±----±--------±------+
| SubjectNo | int(11) | NO | | NULL | |
| SubjectName | varchar(50) | YES | | NULL | |
| ClassHour | int(4) | YES | | NULL | |
| GrandID | int(4) | YES | | NULL | |
±------------±------------±-----±----±--------±------+
4 rows in set (0.00 sec)

mysql> desc grade;
±----------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±----------±------------±-----±----±--------±------+
| GradeID | int(4) | NO | PRI | NULL | |
| GradeName | varchar(10) | YES | | NULL | |
±----------±------------±-----±----±--------±------+
2 rows in set (0.00 sec)

插入
mysql> use MySchool
mysql> desc grade;
±----------±------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±----------±------------±-----±----±--------±------+
| GradeID | int(4) | NO | PRI | NULL | |
| GradeName | varchar(10) | YES | | NULL | |
±----------±------------±-----±----±--------±------+
2 rows in set (0.00 sec)
mysql> insert into grade values(1,‘大一’);
mysql> insert into grade(GradeID) values(2);
mysql> insert into grade values(3,‘大三’),(4,‘大四’);
mysql> select * from grade;
±--------±----------+
| GradeID | GradeName |
±--------±----------+
| 1 | 大一 |
| 2 | NULL |
| 3 | 大三 |
| 4 | 大四 |
±--------±----------+
4 rows in set (0.00 sec)
如果全部插入可以省略字段名
mysql> insert into grade(GradeName,GradeID) values(‘大五’,5);
mysql> update grade set GradeName=‘大二’ where GradeID=2;
mysql> select * from grade;
±--------±----------+
| GradeID | GradeName |
±--------±----------+
| 1 | 大一 |
| 2 | 大二 |
| 3 | 大三 |
| 4 | 大四 |
| 5 | 大五 |
±--------±----------+
5 rows in set (0.00 sec)

truncat TABLE SUBJECT; 删除表内容,把所有自增的全部清空到0

update student set stu_name=’杨康’,phone=’13500000018’,address=’杨家庄’,emil=’[email protected]’,identityCard=’320114202008123356’ where stu_id=1012;

update student set stu_name=’黄蓉’,phone=’13500000028’,address=’桃花岛’,emil=’ [email protected]’,identityCard=’320114202008125560’ where stu_id=1013;
mysql> delete from student where stu_id%2=0;
mysql> create table test_d(tid int(11) unsigned auto_increment primary key,tname varchar(10));
mysql> insert into test_d(tname) values(‘一’),(‘二’),(‘三’);
mysql> select * from test_d;
±----±------+
| tid | tname |
±----±------+
| 1 | 一 |
| 2 | 二 |
| 3 | 三 |
±----±------+
3 rows in set (0.00 sec)

mysql> delete from test_d where tid=3;
mysql> insert into test_d(tname) values(‘四’),(‘五’),(‘六’);
mysql> select * from test_d;
±----±------+
| tid | tname |
±----±------+
| 1 | 一 |
| 2 | 二 |
| 4 | 四 |
| 5 | 五 |
| 6 | 六 |
±----±------+
5 rows in set (0.00 sec)

mysql> truncate table test_d;
mysql> insert into test_d(tname) values(‘一’),(‘二’),(‘三’);
mysql> select * from test_d;
±----±------+
| tid | tname |
±----±------+
| 1 | 一 |
| 2 | 二 |
| 3 | 三 |
±----±------+
3 rows in set (0.00 sec)

mysql> create table tmp_testd as select * from test_d;
mysql> select * from tmp_testd;
±----±------+
| tid | tname |
±----±------+
| 1 | 一 |
| 2 | 二 |
| 3 | 三 |
±----±------+
3 rows in set (0.00 sec)

mysql> desc test_d;
±------±-----------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------±-----------------±-----±----±--------±---------------+
| tid | int(11) unsigned | NO | PRI | NULL | auto_increment |
| tname | varchar(10) | YES | | NULL | |
±------±-----------------±-----±----±--------±---------------+
2 rows in set (0.00 sec)

mysql> desc tmp_testd;
±------±-----------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±-----------------±-----±----±--------±------+
| tid | int(11) unsigned | NO | | 0 | |
| tname | varchar(10) | YES | | NULL | |
±------±-----------------±-----±----±--------±------+
2 rows in set (0.00 sec)

mysql> show create table tmp_testd;
±----------±--------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±----------±--------------------------------------------------------------------------------------------------------------------------------------------------+
| tmp_testd | CREATE TABLE tmp_testd (
tid int(11) unsigned NOT NULL DEFAULT ‘0’,
tname varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
±----------±--------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> drop table tmp_testd;
Query OK, 0 rows affected (0.00 sec)

mysql> create table tmp_testd as select * from test_d where 1=0;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc tmp_testd;
±------±-----------------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±-----------------±-----±----±--------±------+
| tid | int(11) unsigned | NO | | 0 | |
| tname | varchar(10) | YES | | NULL | |
±------±-----------------±-----±----±--------±------+
2 rows in set (0.00 sec)

1、表已经存在的情况下加外键:
alter table 表1 add constraint 外键名
foreign key (表1的字段) references 表2(表2的字段)

DROP TABLE grade;
ALTER TABLE subject DROP FOREIGN KEY grade_sub_id; //干掉外键名再删掉

insert into student values(
1011,‘郭靖’,‘男’,1,13500000000,
‘北京’,‘[email protected]’,‘320114202008121123’),
(1012,‘李文才’,‘男’,1,13500000000,
‘河南洛阳’,‘[email protected]’,‘320114202008122234’),
(1013,‘李梅’,‘女’,1,13500000000,
‘上海卢湾区’,‘[email protected]’,‘320114202008122345’);

insert into subject values(
1,‘高等数学-1’,120,1),(
2,‘高等数学-2’,110,2),(
3,‘高等数学-3’,100,3),(
4,‘高等数学-4’,130,4);

update
DML语句
对数据进行增删改
values(),(),()
update set 列=’’,列=’’

ALTER TABLE subject ADD
CONSTRAINT grade_sub_id
FOREIGN KEY (GradeID)
REFERENCES grade(GradeID);

DROP TABLE grade;
ALTER TABLE subject DROP FOREIGN KEY grade_sub_id;

CREATE TABLE grade(
GradeID INT(4) PRIMARY KEY,
GradeName VARCHAR(10)
);
#外键约束 约束还在主表不能删除,

DROP TABLE subject;
CREATE TABLE subject(
SubjectNo INT(11) PRIMARY KEY,
SubjectName VARCHAR(10),
ClassHour INT(4),
GradeID INT(4),
CONSTRAINT grade_sub_id FOREIGN KEY(GradeID)
REFERENCES grade(GradeID)
);
#外键约束应该在设计表的时候就定好,不然可能加不了

#information_schema里面 字符编码 KEY_COLUMN_USAGE

SELECT * FROM KEY_COLUMN_USAGE;

#找到外键

SELECT TABLE_SCHEMA,table_name,constraint_name,
Referenced_table_schema,referenced_table_name
FROM key_column_usage WHERE referenced_table_name=‘grade’;

#alter table 表名 drop constraint 外键约束名constraint_name

#有可能多个库里有相同的表名,把MySchool也加上去
DROP TABLE student;

CREATE TABLE student(
stu_id INT(11),
stu_name VARCHAR(10),
gender TINYINT(1),
grade_id INT(4),
phone VARCHAR(11),
address VARCHAR(200),
email VARCHAR(50),
identityCard VARCHAR(18)
)

ALTER TABLE student MODIFY gender VARCHAR(1);

INSERT INTO student VALUES(
1011,‘郭靖’,‘男’,1,13500000000,
‘北京’,‘[email protected]’,‘320114202008121123’),
(1012,‘李文才’,‘男’,1,13500000000,
‘河南洛阳’,‘[email protected]’,‘320114202008122234’),
(1013,‘李梅’,‘女’,1,13500000000,
‘上海卢湾区’,‘[email protected]’,‘320114202008122345’);

ALTER TABLE student MODIFY stu_id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY;

SELECT * FROM SUBJECT;

truncat TABLE SUBJECT;

INSERT INTO SUBJECT VALUES(
1,‘高等数学-1’,120,1),(
2,‘高等数学-2’,110,2),(
3,‘高等数学-3’,100,3),(
4,‘高等数学-4’,130,4);

UPDATE student SET stu_name=’’
UPDATE student SET phone=‘13500000001’,address=‘杨家庄’ WHERE stu_id <>1013;

#会先执行前面的select *
SELECT * FROM student WHERE grade_id>1 AND grade_id <=3;
#只在grade_id列上找,检索的东西少,速度快
SELECT * FROM student WHERE grade_id BETWEEN 2 AND 3;

UPDATE subject SET ClassHour=ClassHour*2 WHERE ClassHour>60 AND GradeID>2;

ALTER TABLE student ADD CONSTRAINT k_stu_grade FOREIGN KEY(grade_id) REFERENCES grade(GradeID);

DESC student;
DESC subject;

#终极解决方案 外键不建了,人为代码控制
DROP TABLE result;
CREATE TABLE result(
stu_id INT(11) UNSIGNED,
sub_no INT(11),
exam_date TIMESTAMP,
score INT(4)
#constraint k_result_stu foreign key(stu_id) references student(stu_id),
#constraint k_result_sub foreign key (sub_no) references subject(SubjectNo)
);

你可能感兴趣的:(笔记)