MySQL 小知识点总结

MySQL是最流行的关系型数据库管理系统,在web应用方面,MySQL是最好的RDBMS(关系数据库管理系统)应用软件之一

数据库是按照数据结构来组织,存储和管理数据的仓库

每个数据库都有一个或多个不同的api用于创建,访问,管理,搜索和复制所保存的数据

1.查找mysql里面有多少数据库   show  databases;

2.创建自己的数据库    create database  student;

3.查看自己的数据库有没有创建出来   show  databases;

4.定位到自己创建的数据库    use  student;

5.查看当前的数据库里面有没有表格(数据表)  show tables;

mysql> use student;//定位到自己创建的数据库

mysql> show tables;

| Tables_in_student |

| user              |

1 row in set (0.00 sec)

6.开始创建表create table user(

   格式:列 列的类型  是否为空 

   id int not null,

   name varchar(20) not null,

   sex int not null,

   age int not null,

   tel varchar(20) not null,

   email varchar(20) not null,

   address varchar(20) not null

  );

7. 创建完表  开始给表里面添加数据

  1.单行插入数据

  insert into user (id,name,sex,age,tel,email,address)

  values

 (1,"张三",0,18,"13767677867","[email protected]","西安市碑林区");

  2.多行插入

  insert into user (id,name,sex,age,tel,email,address)

  values

 (1,"张三",0,18,"13767677867","[email protected]","西安市碑林区"),

 (2,"张三",0,18,"13767677867","[email protected]","西安市碑林区"),

 (3,"张三",0,18,"13767677867","[email protected]","西安市碑林区");

8.查当前表里面有没有数据

  select * from user; 

  条件查询:

  select * from user where id=2;

9.数据表的增删改查

  查: select * from user;

  删: delete from user;  ( 指的是删除表里面的所有数据)

       delete from user where id=2  (根据条件删除)

  改: update user set name="马六"; 没有条件修改整个数据表里面的整个name列

       update user set name="王尼玛" where id=1;根据条件来修改

        增: insert into user  (id,name,sex,age,tel,email,address)

        values

        (1,"张三",0,18,"13767677867","[email protected]","西安市碑林区"),

        (2,"张三",0,18,"13767677867","[email protected]","西安市碑林区"),

        (3,"张三",0,18,"13767677867","[email protected]","西安市碑林区");

        还有单行输入。。。。。

10.给表添加主键

 一种是在创建表的时候添加 (primary key)

删除表的语法:drop table name;

mysql> create table score(

    -> id int primary key,

    -> chinese varchar(20) not null,

    -> english varchar(20) not null,

    -> math varchar(20) not null,

    -> history varchar(20) not null);

Query OK, 0 rows affected (0.09 sec)

mysql> drop table score;

Query OK, 0 rows affected (0.02 sec)

一种是在表外添加Alter table score add primary key(id);

删除主键  alter table score drop primary key;

mysql> create table score(

    -> id int,

    -> chinese varchar(20) not null,

    -> english varchar(20) not null,

    -> math varchar(20) not null,

    -> history varchar(20) not null);

Query OK, 0 rows affected (0.05 sec)

 

mysql> alter table score add primary key(id);

Query OK, 0 rows affected (0.16 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

mysql> alter table score drop primary key;

Query OK, 0 rows affected (0.15 sec)

Records: 0  Duplicates: 0  Warnings: 0

11.添加外键: 

foreign key(id) references user(id);

Alter table score add foreign key(id) references user(id);

删除外键: alter table score drop foreign key(id);

mysql> alter table user add primary key(id);//先设置主键

mysql> drop table score;//先删除表

mysql> create table score(

    -> id int,

    -> foreign key(id) references user(id),

    -> chinese varchar(20) not null,

    -> english varchar(20) not null,

    -> math varchar(20) not null,

    -> history varchar(20) not null);

//下面代码测试primary key(id)是否为主键

//主键要求不能重复,不能为空

mysql> insert into user(id,name,sex,age,tel,email,address)

    -> values

    -> (2,"老李",1,18,"13878738728","[email protected]","咸阳武功");

mysql> insert into user(id,name,sex,age,tel,email,address)

    -> values

    -> (2,"老李",1,18,"13878738728","[email protected]","咸阳武功");

ERROR 1062 (23000): Duplicate entry '2' for key 'PRIMARY'

mysql> insert into user(id,name,sex,age,tel,email,address)

    -> values

    -> (,"老李",1,18,"13878738728","[email protected]","咸阳武功");

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 '"老李

",1,18,"13878738728","[email protected]","咸阳武功")' at line 3

//下面插入表格,foreign key(id) references user(id),因为score的ID是user的id的外键,所以其id只能添加user的id范围内的

mysql> insert into score(id,chinese,english,math,history)

    -> values

    -> (4,"50","12","147","2");

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f

ails (`student`.`score`, CONSTRAINT `score_ibfk_1` FOREIGN KEY (`id`) REFERENCES

 `user` (`id`))

mysql> insert into score(id,chinese,english,math,history)

    -> values

    -> (1,"50","12","147","2");

Query OK, 1 row affected (0.01 sec)

mysql> insert into score(id,chinese,english,math,history)

    -> values

    -> (2,"99","150","147","40");

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into score(id,chinese,english,math,history)

    -> values

    -> (3,"10","120","107","140");

Query OK, 1 row affected (0.00 sec)

12.添加自增列

   auto_increment必须要求该列是主键(或别的键

mysql> create table teacher(

    -> id int primary key auto_increment,//id不仅是主键还是自增列

    -> userid int,

    -> foreign key(userid) references user(id),//userid是user-id的外键

    -> tname varchar(20));

Query OK, 0 rows affected (0.04 sec)

mysql> insert into teacher(id,userid,tname)

    -> values

    -> (1,1,"毛豆");

Query OK, 1 row affected (0.02 sec)

mysql> insert into teacher(id,userid,tname)

    -> values

    -> (1,2,"毛豆");//主键不能重复,不能为空

ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

mysql> insert into teacher(userid,tname)//id是自增列

    -> values

    -> (2,"毛豆");

Query OK, 1 row affected (0.00 sec)

mysql> insert into teacher(userid,tname)

    -> values

    -> (3,"老吴");

Query OK, 1 row affected (0.01 sec)

//表格显示  user score teacher

mysql> select * from user;

| id | name      | sex | age | tel         | email        | address           

|  1 | 王麻子    |   0 |  18 | 13732132131 | [email protected] | 西安市莲湖区       |

|  2 | 老李      |   1 |  18 | 13878738728 | [email protected] | 咸阳武功           |

|  3 | 马六      |   0 |  18 | 13732132131 | [email protected] | 西安市莲湖区       |

3 rows in set (0.00 sec)

mysql> select * from score;

+------+---------+---------+------+---------+

| id   | chinese | english | math | history |

+------+---------+---------+------+---------+

|    1 | 50      | 12      | 147  | 2       |

|    2 | 99      | 150     | 147  | 40      |

|    3 | 10      | 120     | 107  | 140     |

+------+---------+---------+------+---------+

3 rows in set (0.01 sec)

mysql> select * from teacher;

+----+--------+--------+

| id | userid | tname  |

+----+--------+--------+

|  1 |      1 | 毛豆   |

|  2 |      2 | 毛豆   |

|  3 |      3 | 老吴   |

+----+--------+--------+

3 rows in set (0.00 sec)

mysql> select user.id,user.name,teacher.tname,score.chinese,score.english from user,teacher,score where user.id=score.id and score.id=teacher.userid;

+----+-----------+--------+---------+---------+

| id | name      | tname  | chinese | english |

+----+-----------+--------+---------+---------+

|  1 | 王麻子    | 毛豆   | 50      | 12      |

|  2 | 老李      | 毛豆   | 99      | 150     |

|  3 | 马六      | 老吴   | 10      | 120     |

+----+-----------+--------+---------+---------+

3 rows in set (0.01 sec)

猜你喜欢

转载自blog.csdn.net/Auroraone/article/details/81158393