day 37 數據庫MySQL基本操做

一、建立數據庫python

    1.1 語法mysql

      CREATE DATABASE 數據庫名 charset utf8;sql

    1.2 數據庫命名規則數據庫

      能夠由字母、數字、下劃線、@、#、$code

      區分大小寫ip

      惟一性rem

      不能使用關鍵字如 create selectinnodb

      不能單獨使用數字table

      最長128位select

      # 基本上跟python或者js的命名規則同樣

查看數據庫
  show databases;
  show create database db1;
  select database();
選擇數據庫
  USE 數據庫名
刪除數據庫
  DROP DATABASE 數據庫名;
修改數據庫
  alter database db1 charset utf8;

二 . 表操做

  建立表:

mysql> create database db1 charset utf8;

mysql> use db1;

mysql> create table t1(
-> id int,
-> name varchar(50),
-> sex enum('male','female'),
-> age int(3)
-> );

mysql> show tables; #查看db1庫下全部表名

mysql> desc t1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+

mysql> select id,name,sex,age from t1;
Empty set (0.00 sec)

mysql> select * from t1;
Empty set (0.00 sec)

mysql> select id,name from t1;
Empty set (0.00 sec)

代碼示例


#語法: create table 表名( 字段名1 類型[(寬度) 約束條件], 字段名2 類型[(寬度) 約束條件], 字段名3 類型[(寬度) 約束條件] );

#注意: 1. 在同一張表中,字段名是不能相同 2. 寬度和約束條件可選、非必須,寬度指的就是字段長度約束,例如:char(10)裏面的10 3. 字段名和類型是必須的

插入表格的數據:

  

mysql> insert into t1 values
    -> (1,'chao',18,'male'),
    -> (2,'sb',81,'female')
    -> ;
mysql> select * from t1;
+------+------+------+--------+
| id   | name | age  | sex    |
+------+------+------+--------+
|    1 | chao |   18 | male   |
|    2 | sb |   81 | female |
+------+------+------+--------+



mysql> insert into t1(id) values 
    -> (3),
    -> (4);
mysql> select * from t1;
+------+------+------+--------+
| id   | name | age  | sex    |
+------+------+------+--------+
|    1 | chao |   18 | male   |
|    2 | sb |   81 | female |
|    3 | NULL | NULL | NULL   |
|    4 | NULL | NULL | NULL   |
+------+------+------+--------+

 查看錶結構:

mysql> describe t1; #查看錶結構,可簡寫爲:desc 表名
+-------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| sex | enum('male','female') | YES | | NULL | |
| age | int(3) | YES | | NULL | |
+-------+-----------------------+------+-----+---------+-------+


mysql> show create table t1\G; #查看錶詳細結構,可加\G

查看錶結構的方法

注意:在MySQL裏面是不區分大小寫的,好比若是你將你的表名t1輸入成T1是徹底沒用的,由於在數據庫裏面的表名都是小寫的。

修改表的操做:

1. 修改存儲引擎
mysql> alter table service 
    -> engine=innodb;

2. 添加字段
mysql> alter table student10
    -> add name varchar(20) not null,
    -> add age int(3) not null default 22;
    
mysql> alter table student10
    -> add stu_num varchar(10) not null after name;                //添加name字段以後

mysql> alter table student10                        
    -> add sex enum('male','female') default 'male' first;          //添加到最前面

3. 刪除字段
mysql> alter table student10
    -> drop sex;

mysql> alter table service
    -> drop mac;

4. 修改字段類型modify
mysql> alter table student10
    -> modify age int(3);
mysql> alter table student10
    -> modify id int(11) not null primary key auto_increment;    //修改成主鍵

5. 增長約束(針對已有的主鍵增長auto_increment)
mysql> alter table student10 modify id int(11) not null primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined

mysql> alter table student10 modify id int(11) not null auto_increment;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

6. 對已經存在的表增長複合主鍵
mysql> alter table service2
    -> add primary key(host_ip,port);        

7. 增長主鍵
mysql> alter table student1
    -> modify name varchar(10) not null primary key;

8. 增長主鍵和自動增加
mysql> alter table student1
    -> modify id int not null primary key auto_increment;

9. 刪除主鍵
a. 刪除自增約束
mysql> alter table student10 modify id int(11) not null; 

b. 刪除主鍵
mysql> alter table student10                                 
    -> drop primary key;
相關文章
相關標籤/搜索