1. 程式人生 > >SQL 增加或刪除一列

SQL 增加或刪除一列

alter table tablename drop column columnname;
alter table tabelname add columnname varchar2(8) NULL;
一 . 常用mysql命令列命令
       1 .啟動MYSQL服務 net start mysql

停止MYSQL服務 net stop mysql


       2 . netstat –na | findstr 3306 檢視被監聽的埠 , findstr用於查詢後面的在埠是否存在


       3 . 在命令列中登陸MYSQL控制檯 , 即使用 MYSQL COMMEND LINE TOOL

              語法格式 mysql –user=root –password=123456 db_name

                       或 mysql –uroot –p123456 db_name


       4 . 進入MYSQL命令列工具後 , 使用status; 或\s 檢視執行環境資訊


       5 . 切換連線資料庫的語法 : use new_dbname;

      

6 . 顯示所有資料庫 : show databases;


7 . 顯示資料庫中的所有表 : show tables;


8 . 顯示某個表建立時的全部資訊 : show create table table_name;


9 . 查看錶的具體屬性資訊及表中各欄位的描述

       Describe table_name; 縮寫形式 : desc table_name;


二 . MySql中的SQL語句
       1 . 資料庫建立 : Create database db_name;

資料庫刪除 : Drop database db_name; 刪除時可先判斷是否存在,寫成 : drop database if exits db_name

      

2 . 建表 : 建立資料表的語法 : create table table_name (欄位1 資料型別 , 欄位2 資料型別);

                       例 : create table mytable (id int , username char(20));

          刪表 : drop table table_name; 例 : drop table mytable;


       8 . 新增資料 : Insert into 表名 [(欄位1 , 欄位2 , ….)] values (值1 , 值2 , …..);

如果向表中的每個欄位都插入一個值,那麼前面 [ ] 括號內欄位名可寫也可不寫

               例 : insert into mytable (id,username) values (1,’zhangsan’);


       9 . 查詢 : 查詢所有資料 : select * from table_name;

查詢指定欄位的資料 : select 欄位1 , 欄位2 from table_name;

例 : select id,username from mytable where id=1 order by desc;

多表查詢語句------------參照第17條例項
       10 . 更新指定資料 , 更新某一個欄位的資料(注意,不是更新欄位的名字)

Update table_name set 欄位名=’新值’ [, 欄位2 =’新值’ , …..][where id=id_num] [order by 欄位 順序]

例 : update mytable set username=’lisi’ where id=1;

Order語句是查詢的順序 , 如 : order by id desc(或asc) , 順序有兩種 : desc倒序(100—1,即從最新數

據往後查詢),asc(從1-100)

                     Where和order語句也可用於查詢select 與刪除delete


11 . 刪除表中的資訊 :

              刪除整個表中的資訊 : delete from table_name;

              刪作表中指定條件的語句 : delete from table_name where 條件語句 ; 條件語句如 : id=3;

      

       12 . 建立資料庫使用者

              CREATE USER username1 identified BY ‘password’ , username2 IDENTIFIED BY ‘password’….

                     一次可以建立多個數據庫使用者


       13 . 使用者的許可權控制:grant

              庫,表級的許可權控制 : 將某個庫中的某個表的控制權賦予某個使用者

                     Grant all ON db_name.table_name TO user_name [ indentified by ‘password’ ];

        14 . 表結構的修改
              ① 增加一個欄位格式:

alter table table_name add column (欄位名 欄位型別); ----此方法帶括號

                     指定欄位插入的位置:

alter table table_name add column 欄位名 欄位型別 after 某欄位;

② 刪除一個欄位:

alter table table_name drop欄位名;

             

③ 修改欄位名稱/型別

alter table table_name change 舊欄位名 新欄位名 新欄位的型別;

              ④ 改表的名字

alter table table_name rename to new_table_name;

              ⑤ 一次性清空表中的所有資料

truncate table table_name; 此方法也會使表中的取號器(ID)從1開始

                    

15 . 增加主鍵,外來鍵,約束,索引。。。。(使用方法見17例項)
① 約束(主鍵Primary key、唯一性Unique、非空Not Null)


② 自動增張 auto_increment


③外來鍵Foreign key-----與reference table_name(col_name列名)配合使用,建表時單獨使用


④ 刪除多個表中有關聯的資料----設定foreign key 為set null ---具體設定參考幫助文件

      

       16 . 檢視資料庫當前引擎

                     SHOW CREATE TABLE table_name;

              修改資料庫引擎

                     ALTER TABLE table_name ENGINE=MyISAM | InnoDB;

17 . 一個SQL語句運用例項:--1 建users表create table users (id int primary key auto_increment,nikename varchar(20) not null unique,password varchar(100) not null,address varchar(200)); --2 建articles表,在建表時設定外來鍵create table articles (id int primary key auto_increment,content longtext not null,userid int,constraint foreign key (userid) references users(id) on delete set null); ----------------------------------------------------------------------- --2.1 建articles表,建表時不設定外來鍵 create table articles (id int primary key auto_increment,content longtext not null,userid int); --2.2 給articles表設定外來鍵 alter table articles add constraint foreign key (userid) references users(id) on delete set null;------------------------------------------------------------------------ --3. 向users表中插入資料,同時插入多條insert into users (id,nikename,password,address) values (1,'lyh1','1234',null),(10,'lyh22','4321','湖北武漢'), (null,'lyh333','5678','北京海淀'); --4. 向article中插入三條資料insert into articles (id,content,userid) values (2,'hahahahahaha',11),(null,'xixixixixix',10),(13,'aiaiaiaiaiaiaiaiaiaiaiaia',1),(14,'hohoahaoaoooooooooo',10); --5. 進行多表查詢,選擇users表中ID=10的使用者釋出的所有留言及該使用者的所有資訊select articles.id,articles.content,users.* from users,articles where users.id=10 and articles.userid=users.id order by articles.id desc; --6. 檢視資料庫引擎型別show create table users; --7. 修改資料庫引擎型別alter table users engine=MyISAM; ---因為users表中ID被設定成外來鍵,執行此句會出錯 --8. 同表查詢,已知一個條件的情況下.查詢ID號大於使用者lyh1的ID號的所有使用者select a.id,a.nikename,a.address from users a,users b where b.nikename='lyh1' and a.id>b.id;------也可寫成