【Mysql】數據庫的命名規則 與 表的增刪改查


數據庫命名規則:

  1. 可以由字符、數字、下划線、 @、#、$組成
  2. 區分大小寫
  3. 不能使用關鍵字
  4. 不能單獨使用數字
  5. 最長128位

 

創建表

create table 表名(

字段名1 類型[(寬度) 約束條件],

字段名2 類型[(寬度) 約束條件],

字段名3 類型[(寬度) 約束條件]

);

注意: 

1.字段名不可相同 

2.寬度和約束條件可選

3.字段名和類型是必須的

 

刪除表

drop table;

truncate db1.t1清空表數據

 

查看表

show tables; 查看操作庫下的所有表

show create table t1查看表內所有

show create table t1\G無格式按行查看

desc t1;  查看目前庫的t1表所有字段

select id,name from t1;  查看表中對應的字段數據

 

修改表結構

1.修改表名 ALTER table 表名 rename 新表名

 

2.增加字段 alter table 表名 add 字段名1 數據類型[完整性約束條件],

            add 字段名2 數據類型[完整性約束條件] [FIRST],

            add 字段名3 數據類型[完整性約束條件] [AFTER 字段名];

 增加約束 alter table 表名 add foreign key 約束名(約束對象) reference 被關聯表名(被關聯字段) on ;

     alter table 表名 add primary key 約束名(約束對象) on delete cascade on update cascade;

 

3.刪除字段 alter table 表名 drop 字段名;

   刪除約束 alter table 表名 drop foreign key 約束名;     # 約束名可在show create table中看到    

 

4.修改字段alter table 表名 modify 字段名 數據類型[條件];

     alter table 表名 change 舊字段名 新字段名 舊數據類型[條件];

     alter table 表名 change 舊字段名 新字段名 新數據類型[條件];

 修改約束 alter table 表名 modify foreign key 約束名(約束對象) reference 被關聯表名(被關聯字段) on ;

     alter table 表名 modify primary key 約束名(約束對象) on delete cascade on update cascade;

 

5.復制表 create table t1 setect host,user from mysql.user where 1;

     create table t1 setect host,user from mysql.use;

   復制表結構 create table t1 select host,user from mysql.user where 0;

                     create table t1 like mysql.user;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM