mysql/mariadb學習記錄——建立刪除資料庫、表的基本命令

這個暱稱得改發表於2018-04-30

檢視已有的資料庫:

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

新建資料庫語句:

mysql> create database wzu;
Query OK, 1 row affected (0.05 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
| wzu                |
+--------------------+
3 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, 0 rows affected (0.41 sec)

檢視資料庫中有哪些表格:

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

更改外來鍵約束:

1 set foreign_key_checks=0;  //忽略外來鍵檢查
2 set foreign_key_checks=1; //重啟外來鍵檢查

更改表中某屬性的資料型別:

命令:alter table <表名> modify <需要更改的屬性> <想要更改的型別>;
如:student 表中的sno欄位改成varchar(11)

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

可以用desc <表名> 來檢視錶格結構:

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

表格插入資料:

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

1 insert into student values(`1024`,`測試`,``,`18`,`CS`);

刪除一行資料:

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

1 mysql> delete from emp where empno=`232`;

修改一行資料:

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

1 mysql> update course set cpno=null where cpno=`NULL`;

 

表格中的簡單資料查詢:

select * from [表名]
如:select * from student;
* 表示查詢表格中所有的列,也可以用列名加`,`隔開來選擇自己需要的資料

mysql> select * from student;
+-------------+--------+------+------+-------+
| sno         | sname  | ssex | sage | sdept |
+-------------+--------+------+------+-------+
| 1024        | 測試    | 男   |   18 | CS    |
| 1621116     | 周帥    | 男   |   21 | CS    |
+-------------+--------+------+------+-------+

mysql> select sno,sname from student;
+-------------+--------+
| sno         | sname  | 
+-------------+--------+
| 1024        | 測試    |
| 1621116     | 周帥    |
+-------------+--------+

給已建立的表格增加一欄屬性:

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

mysql> alter table dept add type varchar(11);
Query OK, 4 rows affected (0.62 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> desc dept;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| deptno | decimal(2,0) | NO   | PRI | NULL    |       |
| dname  | varchar(14)  | NO   |     | NULL    |       |
| loc    | varchar(13)  | YES  |     | NULL    |       |
| type   | varchar(11)  | YES  |     | NULL    |       |
+--------+--------------+------+-----+---------+-------+

在已建立的表格中刪除一欄屬性:

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

mysql> alter table dept drop type;
Query OK, 4 rows affected (0.57 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> desc dept;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| deptno | decimal(2,0) | NO   | PRI | NULL    |       |
| dname  | varchar(14)  | NO   |     | NULL    |       |
| loc    | varchar(13)  | YES  |     | NULL    |       |
+--------+--------------+------+-----+---------+-------+

當發現一個操作有warning時 可以通過show warnings;來檢視警告資訊:

mysql> show warnings;
+---------+------+-----------------------------------------------------------------------------------+
| Level   | Code | Message                                                                           |
+---------+------+-----------------------------------------------------------------------------------+
| Warning | 1366 | Incorrect string value: `xE6x9Dx8ExE5x8Bx87` for column `sname` at row 1    |
| Warning | 1366 | Incorrect string value: `xE7x94xB7` for column `ssex` at row 1                 |
| Warning | 1366 | Incorrect string value: `xE6x95xB0xE6x8DxAE...` 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 |
+-----+--------------+------+---------+
| 2   | 高等數學      | NULL |       2 |
| 6   | 資料處理      | NULL |       2 |
+-----+--------------+------+---------+
2 rows in set (0.05 sec)

mysql> select * from course where cpno is not null;
+-----+--------------+------+---------+
| cno | cname        | cpno | ccredit |
+-----+--------------+------+---------+
| 1   | 資料庫        | 5    |       4 |
| 3   | 資訊系統      | 1    |       4 |
| 4   | 作業系統      | 6    |       3 |
| 5   | 資料結構      | 7    |       4 |
| 7   | C語言         | 6    |       4 |
+-----+--------------+------+---------+

 

相關文章