mysql学习03
Posted 独孤_败天
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql学习03相关的知识,希望对你有一定的参考价值。
https://www.cnblogs.com/wupeiqi/articles/5729934.html
https://www.cnblogs.com/wupeiqi/articles/5748496.html
mysql测试题
一、表关系
请创建如下表,并创建相关约束
Microsoft Windows [版本 10.0.14393] (c) 2016 Microsoft Corporation。保留所有权利。 C:\\Users\\Administrator>mysql -u root -P3306 -h 127.0.0.1 ERROR 1045 (28000): Access denied for user \'root\'@\'localhost\' (using password: NO) C:\\Users\\Administrator>mysql -u root -P3306 -h 127.0.0.1 -p Enter password: ****** Welcome to the MySQL monitor. Commands end with ; or \\g. Your MySQL connection id is 2 Server version: 5.5.13 MySQL Community Server (GPL) Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | kaige | | mysql | | performance_schema | | runbs | | sdhz | | sunck | | test | +--------------------+ 8 rows in set (0.17 sec) mysql> create database sql_test -> ; Query OK, 1 row affected (0.07 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | kaige | | mysql | | performance_schema | | runbs | | sdhz | | sql_test | | sunck | | test | +--------------------+ 9 rows in set (0.00 sec) mysql> use sql_test; Database changed mysql> drop database sql_test; Query OK, 0 rows affected (0.21 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | kaige | | mysql | | performance_schema | | runbs | | sdhz | | sunck | | test | +--------------------+ 8 rows in set (0.00 sec) mysql> create database sql_test default charset utf-8 collate utf8_general_ci; ERROR 1115 (42000): Unknown character set: \'utf\' mysql> create database sql_test default charset utf8 collate utf8_general_ci; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | kaige | | mysql | | performance_schema | | runbs | | sdhz | | sql_test | | sunck | | test | +--------------------+ 9 rows in set (0.00 sec) mysql> use sql_test; Database changed mysql> show tables; Empty set (0.05 sec) mysql> create table class( -> cid int not null auto_increment primary key, -> caption varchar(25) -> )engine=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.16 sec) mysql> show tables; +--------------------+ | Tables_in_sql_test | +--------------------+ | class | +--------------------+ 1 row in set (0.00 sec) mysql> desr class -> ; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'desr class\' at line 1 mysql> desc class; +---------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+----------------+ | cid | int(11) | NO | PRI | NULL | auto_increment | | caption | varchar(25) | YES | | NULL | | +---------+-------------+------+-----+---------+----------------+ 2 rows in set (0.09 sec) mysql> create table teacher( -> tid int auto_increment primary key not null, -> tname varchar(25), -> )engine=InnoDB default charset=utf8; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \')engine=InnoDB default charset=utf8\' at line 4 mysql> create table teacher( -> tid int auto_increment primary key not null, -> tname varchar(25) -> )engine=InnoDB default charset=utf8; Query OK, 0 rows affected (0.11 sec) mysql> desc teacher -> ; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | tid | int(11) | NO | PRI | NULL | auto_increment | | tname | varchar(25) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec) mysql> create table student( -> sid int auto_increment primary key not null, -> sname varchar(25), -> gender varchar(2), -> class_id int -> )engine=InnoDB default charset=utf8; Query OK, 0 rows affected (0.09 sec) mysql> alter table class add constraint FK_class_student foreign key class.cid references student.class_id; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'references student.class_id\' at line 1 mysql> show tables; +--------------------+ | Tables_in_sql_test | +--------------------+ | class | | student | | teacher | +--------------------+ 3 rows in set (0.00 sec) mysql> desc student; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | sid | int(11) | NO | PRI | NULL | auto_increment | | sname | varchar(25) | YES | | NULL | | | gender | varchar(2) | YES | | NULL | | | class_id | int(11) | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec) mysql> desc class; +---------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+----------------+ | cid | int(11) | NO | PRI | NULL | auto_increment | | caption | varchar(25) | YES | | NULL | | +---------+-------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec) mysql> alter table student add constraint fk_stu_cls foreign key student(class_id) references class(cid); Query OK, 0 rows affected (0.38 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | sid | int(11) | NO | PRI | NULL | auto_increment | | sname | varchar(25) | YES | | NULL | | | gender | varchar(2) | YES | | NULL | | | class_id | int(11) | YES | MUL | NULL | | +----------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql> create table student( -> sid int not null primary key auto_increment, -> sname varchar(25), -> gender varchar(25), -> class_id;;;; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'\' at line 5 ERROR: No query specified ERROR: No query specified ERROR: No query specified mysql> create table course9 -> create table course9;; -> -> ‘ -> ; -> -> ); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'create table course9???? ?? ?? )\' at line 2 mysql> create table course( -> cid int not null primary key auto_increment, -> cname varchar(25), -> tearch_id int -> )engine=InnoDB default charset=utf8; Query OK, 0 rows affected (0.09 sec) mysql> alter table course add constraint fk_cour_teach foreign key course(tearch_id) references teacher(tid); Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> create table score( -> sid int not null primary key auto_increment, -> student_id int not null, -> corse_id int not null, -> number int not null -> )engine=InnoDB default charset=utf8; Query OK, 0 rows affected (0.13 sec) mysql> alter table score add constraint score_to_stu foreign key score(student_id) references student(sid); Query OK, 0 rows affected (0.23 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table score add constraint score_to_corse foreign key score(corse_id) references course(cid); Query OK, 0 rows affected (0.45 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql>
mysql> select -> * -> from -> score -> left join student on score.student_id=student.sid -> left join course on score.corse_id=course.cid; +-----+------------+----------+--------+------+-------+--------+----------+------+---------+-----------+ | sid | student_id | corse_id | number | sid | sname | gender | class_id | cid | cname | tearch_id | +-----+------------+----------+--------+------+-------+--------+----------+------+---------+-----------+ | 1 | 1 | 1 | 60 | 1 | may | nv | 1 | 1 | shengwu | 1 | | 2 | 1 | 2 | 59 | 1 | may | nv | 1 | 2 | tiyu | 1 | | 3 | 2 | 2 | 100 | 2 | tom | na | 1 | 2 | tiyu | 1 | +-----+------------+----------+--------+------+-------+--------+----------+------+---------+-----------+ 3 rows in set (0.06 sec) mysql> select -> score.sid, -> score.student_id, -> student.sname, -> score.corse_id, -> course.cname, -> score.name -> from -> left join student on score.student_idIOS开发-OC学习-常用功能代码片段整理连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)(代码片段