ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

mySQL学习笔记二

2020-01-23 14:43:32  阅读:211  来源: 互联网

标签:set mySQL 笔记 学习 stu sec mysql YES NULL


DDL:操作数据库,表
CRUD 操作数据库
1.C(create):创建
2.R(Retrieve):查
3.U(Update) :修改
4.D(Delete):删除
5.使用数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
mysql> -- 前三个最好数据库不要动
mysql> show create  database  mysql;
+----------+----------------------------------------------------------------+
| Database | Create Database                                                |
+----------+----------------------------------------------------------------+
| mysql    | CREATE DATABASE `mysql` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> -- 查看某个数据库的字符集//创建语句  create database +数据库名称;
mysql> -- 创建数据库如果不存在就创建,如果存在也不会报错
mysql> create database if not exists db1;
Query OK, 1 row affected (0.01 sec)
mysql> -- 创建db2在其不存在的情况下,并指定字符集为gbk
mysql> create database if not exists db2 character set gbk;
Query OK, 1 row affected (0.00 sec)
mysql> -- 修改字符集
mysql> alter database db2 character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> -- 删除db3
mysql> drop database if exists db3;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> -- 使用数据库
mysql> -- 查询当前正在使用的数据库
mysql> select database();
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

mysql> use db2;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| db2        |
+------------+
1 row in set (0.00 sec)

CRUD 操作表
1.C(create):创建
2.R(Retrieve):查
3.U(Update) :修改
4.D(Delete):删除

mysql> create table 表名(
         列名1  数据类型1,
         列名2  数据类型2,
         .....
         列名n  数据类型n
         );
mysql> -- 注意:最后一列不加逗号
mysql> /* 数据库类型:1.int 整数类型: age int,
                    2.double 小数类型: score double(5, 2)
                    3.date: 日期,只包含年月日  yyyy-MM-dd
                    4.datetime:日期,年月日时分秒 yyyy-MM-dd
                    HH:mm:ss 如果将来不给这个字段赋值,或者赋值为null,则默认使         用当前的系统时间,来自动赋值
                    5.timestamp 时间戳类型 包含年月日时分秒  yyyy-MM-dd HH:mm:ss 如果将来不给这个字段赋值,或赋值为null,则默认使用当前的系统时间,来自动赋值
                    6.varchar:字符串
                    * name varchar(20):姓名最大20个字符                
                    */
mysql> show tables;
mysql> -- desc是描述description的缩写
mysql> desc 表名;
mysql> create table student(
    -> id int,
    -> name varchar(32),
    -> age int,
    -> score double(4,1),
    -> birthday date,
    -> insert_time timestamp
    -> );
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| student       |
+---------------+
1 row in set (0.00 sec)

mysql> desc student;
+-------------+-------------+------+-----+-------------------+-----------------------------+
| Field       | Type        | Null | Key | Default           | Extra                       |
+-------------+-------------+------+-----+-------------------+-----------------------------+
| id          | int(11)     | YES  |     | NULL              |                             |
| name        | varchar(32) | YES  |     | NULL              |                             |
| age         | int(11)     | YES  |     | NULL              |                             |
| score       | double(4,1) | YES  |     | NULL              |                             |
| birthday    | date        | YES  |     | NULL              |                             |
| insert_time | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-------------+------+-----+-------------------+-----------------------------+
6 rows in set (0.02 sec)
mysql> -- 复制一下学生表
mysql> create table stu like student;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| stu           |
| student       |
+---------------+
2 rows in set (0.00 sec)

mysql> desc stu;
+-------------+-------------+------+-----+-------------------+-----------------------------+
| Field       | Type        | Null | Key | Default           | Extra                       |
+-------------+-------------+------+-----+-------------------+-----------------------------+
| id          | int(11)     | YES  |     | NULL              |                             |
| name        | varchar(32) | YES  |     | NULL              |                             |
| age         | int(11)     | YES  |     | NULL              |                             |
| score       | double(4,1) | YES  |     | NULL              |                             |
| birthday    | date        | YES  |     | NULL              |                             |
| insert_time | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-------------+------+-----+-------------------+-----------------------------+
6 rows in set (0.01 sec)
mysql> drop table stu;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table stu;
ERROR 1051 (42S02): Unknown table 'stu'
mysql> drop table if exists stu;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> -- 修改表的字符集
mysql> alter table stu character set utf8;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| stu           |
+---------------+
1 row in set (0.00 sec)

mysql> -- 添加一列
mysql> alter table stu add gender varchar(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> -- 修改列名称 类型
mysql> alter table stu change gender sex varchar(20);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| stu           |
+---------------+
1 row in set (0.00 sec)

mysql> desc stu;
+-------------+-------------+------+-----+-------------------+-----------------------------+
| Field       | Type        | Null | Key | Default           | Extra                       |
+-------------+-------------+------+-----+-------------------+-----------------------------+
| id          | int(11)     | YES  |     | NULL              |                             |
| name        | varchar(32) | YES  |     | NULL              |                             |
| age         | int(11)     | YES  |     | NULL              |                             |
| score       | double(4,1) | YES  |     | NULL              |                             |
| birthday    | date        | YES  |     | NULL              |                             |
| insert_time | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| sex         | varchar(20) | YES  |     | NULL              |                             |
+-------------+-------------+------+-----+-------------------+-----------------------------+
7 rows in set (0.02 sec)

mysql> alter table stu modify sex varchar(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| stu           |
+---------------+
1 row in set (0.00 sec)

mysql> desc stu;
+-------------+-------------+------+-----+-------------------+-----------------------------+
| Field       | Type        | Null | Key | Default           | Extra                       |
+-------------+-------------+------+-----+-------------------+-----------------------------+
| id          | int(11)     | YES  |     | NULL              |                             |
| name        | varchar(32) | YES  |     | NULL              |                             |
| age         | int(11)     | YES  |     | NULL              |                             |
| score       | double(4,1) | YES  |     | NULL              |                             |
| birthday    | date        | YES  |     | NULL              |                             |
| insert_time | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| sex         | varchar(10) | YES  |     | NULL              |                             |
+-------------+-------------+------+-----+-------------------+-----------------------------+
7 rows in set (0.02 sec)

mysql> -- 删除列
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| stu           |
+---------------+
1 row in set (0.00 sec)

mysql> desc stu;
+-------------+-------------+------+-----+-------------------+-----------------------------+
| Field       | Type        | Null | Key | Default           | Extra                       |
+-------------+-------------+------+-----+-------------------+-----------------------------+
| id          | int(11)     | YES  |     | NULL              |                             |
| name        | varchar(32) | YES  |     | NULL              |                             |
| age         | int(11)     | YES  |     | NULL              |                             |
| score       | double(4,1) | YES  |     | NULL              |                             |
| birthday    | date        | YES  |     | NULL              |                             |
| insert_time | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-------------+------+-----+-------------------+-----------------------------+
6 rows in set (0.02 sec)
只不停 发布了22 篇原创文章 · 获赞 1 · 访问量 1032 私信 关注

标签:set,mySQL,笔记,学习,stu,sec,mysql,YES,NULL
来源: https://blog.csdn.net/weixin_42082088/article/details/104075751

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有