mysql/mariadb学习记录——创建删除数据库、表的基本命令

查看已有的数据库:

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
rows in set (0.05 sec)

新建数据库语句:

mysql> create database wzu;
Query OK, row affected (0.05 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
| wzu |
+--------------------+
rows in set (0.05 sec)

要对某个数据库进行操作时首先要选中这个数据库,使用use <dbname>:

mysql> use wzu
Database changed

创建表格:

命令:create table <表名> ( <属性名 属性类型 [修饰]> );

create table student(sno char(9) primary key,sname char(20) unique,ssex char(2),sage smallint, sdept char(20));
create table course(cno char(4) primary key,cname char(40) not null,cpno char(4),ccredit smallint,foreign key(cpno) references course(cno));
create table sc(sno char(9),cno char(4),grade smallint,primary key(sno,cno),foreign key(sno) references student(sno),foreign key(cno) references course(cno));

删除表格:

命令:drop table <表名>;

mysql> drop table emp;
Query OK, rows affected (0.41 sec)

查看数据库中有哪些表格:

mysql> show tables;
+---------------+
| Tables_in_wzu |
+---------------+
| course |
| sc |
| student |
+---------------+

更改外键约束:

 set foreign_key_checks=;  //忽略外键检查
set foreign_key_checks=; //重启外键检查

更改表中某属性的数据类型:

命令:alter table <表名> modify <需要更改的属性> <想要更改的类型>;
如:student 表中的sno字段改成varchar(11)

mysql> alter table student modify sno varchar();
Query OK, rows affected (0.15 sec)
Records: Duplicates: Warnings:

可以用desc <表名> 来查看表格结构:

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sno | varchar() | NO | PRI | | |
| sname | varchar() | YES | UNI | NULL | |
| ssex | varchar() | YES | | NULL | |
| sage | smallint() | YES | | NULL | |
| sdept | varchar() | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

表格插入数据:

命令:insert into <表名> values( 填,入,相,应,的,值 );

 insert into student values('','测试','男','','CS');

删除一行数据:

命令:delete from <表名> where [条件];

 mysql> delete from emp where empno='';

修改一行数据:

命令:update <表名> set [属性名]=[值] [where] [条件]

 mysql> update course set cpno=null where cpno='NULL';

表格中的简单数据查询:

select * from [表名]
如:select * from student;
* 表示查询表格中所有的列,也可以用列名加','隔开来选择自己需要的数据

mysql> select * from student;
+-------------+--------+------+------+-------+
| sno | sname | ssex | sage | sdept |
+-------------+--------+------+------+-------+
| | 测试 | 男 | | CS |
| | 周帅 | 男 | | CS |
+-------------+--------+------+------+-------+ mysql> select sno,sname from student;
+-------------+--------+
| sno | sname |
+-------------+--------+
| | 测试 |
| | 周帅 |
+-------------+--------+

给已创建的表格增加一栏属性:

alter table <表名> add <属性名> <属性域>;
如:

mysql> alter table dept add type varchar();
Query OK, rows affected (0.62 sec)
Records: Duplicates: Warnings: mysql> desc dept;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| deptno | decimal(,) | NO | PRI | NULL | |
| dname | varchar() | NO | | NULL | |
| loc | varchar() | YES | | NULL | |
| type | varchar() | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+

在已创建的表格中删除一栏属性:

alter table <表名> drop <属性名>;
如:

mysql> alter table dept drop type;
Query OK, rows affected (0.57 sec)
Records: Duplicates: Warnings: mysql> desc dept;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| deptno | decimal(,) | NO | PRI | NULL | |
| dname | varchar() | NO | | NULL | |
| loc | varchar() | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+

当发现一个操作有warning时 可以通过show warnings;来查看警告信息:

mysql> show warnings;
+---------+------+-----------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-----------------------------------------------------------------------------------+
| Warning | 1366 | Incorrect string value: '\xE6\x9D\x8E\xE5\x8B\x87' for column 'sname' at row 1 |
| Warning | 1366 | Incorrect string value: '\xE7\x94\xB7' for column 'ssex' at row 1 |
| Warning | 1366 | Incorrect string value: '\xE6\x95\xB0\xE6\x8D\xAE...' for column 'cname' at row 1 |
+---------+------+-----------------------------------------------------------------------------------+

此处的问题主要是因为插入的编码格式不兼容(插入的数据编码为utf8 表格的编码为latinl)

解决方法见mysql/mariadb学习过程中出现的问题与解决

mysql查看当前选择的数据库:

mysql> select database();
+------------+
| database() |
+------------+
| groupdb |
+------------+
//或者:

mysql> show tables;
+-------------------+
| Tables_in_groupdb |//此处为当前数据库
+-------------------+
| course            |
| dept              |
| emp               |
| jwc               |
| salgrade          |
| sc                |
| student           |
| temp              |
+-------------------+

//或者:

mysql> status;
--------------
mysql Ver 14.14 Distrib 5.7.14, for Win64 (x86_64)

Connection id: 2721
Current database: groupdb //此处为当前数据库
Current user: group@122.228.131.86
SSL: Not in use
Using delimiter: ;
Server version: 5.5.56-MariaDB MariaDB Server
Protocol version: 10
Connection: AAA.AAA.AAA.AAA via TCP/IP
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
Uptime: 2 days 5 hours 25 min 51 sec

Threads: 2 Questions: 549 Slow queries: 0 Opens: 26 Flush tables: 2 Open tables: 44 Queries per second avg: 0.002

2018-04-30

判断一个值是否为NULL:

使用 is 关键字而不能用 = 来判断

mysql> select * from course where cpno is null;
+-----+--------------+------+---------+
| cno | cname | cpno | ccredit |
+-----+--------------+------+---------+
| | 高等数学 | NULL | |
| | 数据处理 | NULL | |
+-----+--------------+------+---------+
rows in set (0.05 sec) mysql> select * from course where cpno is not null;
+-----+--------------+------+---------+
| cno | cname | cpno | ccredit |
+-----+--------------+------+---------+
| | 数据库 | | |
| | 信息系统 | | |
| | 操作系统 | | |
| | 数据结构 | | |
| | C语言 | | |
+-----+--------------+------+---------+
上一篇:Mysql怎么样避免全表扫描,sql查询优化


下一篇:一款免费的js图表工具--morris